數據結構與算法JavaScript描述.

隊列: 先進先出,尾添加,頭刪除.javascript

文件地址java

<script type="text/javascript">
//隊列,先進先出,尾添加,頭刪除.
function Queue() {

    //
    var _enqueue = function(element) {
            this.dataStore.push(element);
        }
        //
    var _dequeue = function() {
            return this.dataStore.shift();
        }
        //
    var _front = function() {
            return this.dataStore[0];
        }
        //
    var _back = function() {
            return this.dataStore[this.dataStore.length - 1]
        }
        //
    var _toString = function() {
            var retStr = "";
            for (var i = 0; i < this.dataStore.length; ++i) {
                retStr += this.dataStore[i] + "\n";
            }
            return retStr;
        }
        //
    var _empty = function() {
        if (this.dataStore.length == 0) {
            return true;
        } else {
            return false;
        }
    }

    //
    this.dataStore = [];
    //
    this.enqueue = _enqueue;
    this.dequeue = _dequeue;
    this.front = _front;
    this.back = _back;
    this.toString = _toString;
    this.empty = _empty;

}

//

var q= new Queue();
q.enqueue('yujj');
q.enqueue('Zzc');
q.enqueue('Fjb');
console.log(q);

q.dequeue();

console.log(q);
//
console.log(q.toString());
//
console.log(q.front());
console.log(q.back());
//
</script>

鏈表node

<script type="text/javascript">

//鏈表,
function Llist(){

    //內存單元
    var Node = function(element) {
        this.element = element;
        this.next = null;
    }

    //查找
    var _find = function(item) {
        var currNode = this.head;
        while (currNode.element != item) {
            currNode = currNode.next;
        }
        return currNode;
    }

    //添加單元
    var _insert = function(newElement, item) {
        var newNode = new Node(newElement);
        var current = this.find(item);
        //
        newNode.next = current.next;
        current.next = newNode;
    }

    //
    var _display = function() {
        var currNode = this.head;
        while (!(currNode.next == null)) {
            currNode = currNode.next;
        }
    }


    //
    var _findPrevious = function(item) {
        var currNode = this.head;
        while (!(currNode.next == null) && (currNode.next.element !== item)) {
            currNode = currNode.next;
        }
        return currNode;
    }
    

    //刪除單元
    var _remove = function(item) {

        var prevNode = this.findPrevious(item);
        if (!(prevNode.next == null)) {
            prevNode.next = prevNode.next.next;
        }
    }

    //
    this.head = new Node("head");
    this.find = _find;
    this.insert = _insert;
    this.remove = _remove;
    this.display = _display;
    this.findPrevious = _findPrevious;
}


//
var cities = new Llist();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Alma", "Russellville");
cities.insert("yuq", "Conway");

//
cities.display();
console.log(cities);
cities.remove('Conway');
console.log(cities);
</script>

棧:後進先出app

<script type="text/javascript">

//
function Stack()
{

    //入棧
    var _push = function(element){
        _this.dataStore[_this.top++] = element;
    }


    //出棧
    var _pop = function(){
        delete _this.dataStore[_this.top-1];
        _this.top--;
        // return data;
    }

    //返回棧頂元素
    var _peek = function(){
        return _this.dataStore[_this.top-1]
    }

    //
    var _clear = function()
    {
        _this.dataStore = [];
        _this.top = 0;
    }

    //
    var _length = function()
    {
        return _this.top;
    }

    //
    this.dataStore = [];
    this.top = 0;
    //
    this.push = _push;
    this.pop = _pop;
    this.peek = _peek;
    this.length = _length;
    this.clear = _clear;
    //
    var _this = this;
}


var s = new Stack();
s.push('Yuq');
s.push('Yujj');
//
console.log(s);
s.pop();
console.log(s);
s.push('Zhcl');
console.log(s.length());
console.log(s);
//
console.log(s.peek());
s.push('Fujb');
console.log(s);
console.log(s.clear());
console.log(s);
console.log(s.peek());
</script>

散列:post

<script type="text/javascript">
function HashTable() {
    
    //
    var _simpleHash = function(data) {
        var total = 0;
        for (var i = 0; i < data.length; ++i) {
            total += data.charCodeAt(i);
        }
        //
        return total % this.table.length;
    }

    //
    var _betterHash = function(string) {
        const H = 37;
        var total = 0;
        //
        for (var i = 0; i < string.length; ++i) {
            total += H * total + string.charCodeAt(i);
        }
        //
        total = total % this.table.length;
        //
        if (total < 0) {
            total += this.table.lenght - 1;
        }
        //
        return parseInt(total);
    }


    //
    var _showDistro = function() {
        var n = 0;
        for (var i = 0; i < this.table.length; ++i) {
            if (this.table[i] != undefined) {
                console.log(this.table[i]);
            }
        }
    }


    //
    var _put = function(data) {
         // var pos=this.simpleHash(data);
         var pos = this.betterHash(data);
        console.log(pos);
        this.table[pos] = data;
    }


    //
    this.table = new Array(137);
    this.simpleHash = _simpleHash;
    this.showDistro = _showDistro;
    this.betterHash = _betterHash;
    this.put = _put;
    //this.get = _get;
}

//
var someNames = ['David', 'Jennifer', 'Donnie', 'Raymond', 'Cynthia', 'Mike', 'Clayton', 'Danny', 'Jonathan'];
var htable = new HashTable();

//
for (var i = 0; i < someNames.length; ++i) {
    htable.put(someNames[i]);
}
//
console.log(htable);
//
htable.showDistro();
</script>

字典ui

<script type="text/javascript">

//字典,key=>val 鍵值對
function Dictionary()
{

    var _add = function(key,val)
    {
        this.datastore[key] = val;
        this.datastore.length++;
    }
    //
    var _find = function(key)
    {
        return this.datastore[key];
    }
    //
    var _remove = function(key)
    {
        delete this.datastore[key];
        this.datastore.length--;
    }
    //
    var _showAll = function()
    {
        for(var key in Object.keys(this.datastore))
        {
            console.log(this.datastore[key]);
        }
    }
    //
    var _count = function()
    {
        var n=0;
        for(var key in Object.keys(this.datastore))
        {
            ++n;
        }
        return n;
    }
    //
    var _clear = function()
    {
        for(var key in this.datastore)
        {
            delete this.datastore[key];
        }
        this.datastore.length = 0;
    }
    //
    this.add = _add;
    this.datastore = new Array();
    this.find = _find;
    this.remove = _remove;
    this.showAll = _showAll;
    this.count = _count;
    this.clear = _clear;
}

//
var pbook = new Dictionary();
pbook.add("raymond",'123');
pbook.add("David",'345');
pbook.add("Cynthia","456");
//
pbook.remove('Cynthia');

//
console.log(pbook);
console.log(pbook.count());
console.log(pbook.find('David'));
//pbook.clear();
//
console.log(pbook);
</script>

雙向鏈表this

<script type="text/javascript">

//雙向鏈表
function Llist(){


    //內存單元
    var Node = function(element) {
        this.element = element;
        this.next = null;
        this.previous =null;
    }


    //查找
    var _find = function(item) {
        var currNode = this.head;
        while (currNode.element != item) {
            currNode = currNode.next;
        }
        return currNode;
    }


    //添加單元
    var _insert = function(newElement, item) {
        var newNode = new Node(newElement);
        var current = this.find(item);
        //
        newNode.next = current.next;
        newNode.previous = current;
        current.next = newNode;
    }

    //
    var _display = function() {
        var currNode = this.head;
        while (!(currNode.next == null)) {
            currNode = currNode.next;
        }
    }


    //
    var _findPrevious = function(item) {
        var currNode = this.head;
        while (!(currNode.next == null) && (currNode.next.element !== item)) {
            currNode = currNode.next;
        }
        return currNode;
    }
    

    //刪除單元
    var _remove = function(item) {

        var currNode = this.find(item);
        if(!(currNode.next == null))
        {
            currNode.previous.next = currNode.next;
            currNode.next.previous = currNode.previous;
            currNode.next = null;
            currNode.previous = null;
        }
    }


    //
    var _findLast = function()
    {
        var currNode = this.head;
        while(!(currNode.next == null))
        {
            currNode = currNode.next;
        }
        return currNode;
    }

    //
    var _dispReverse = function()
    {
        var currNode = this.head;
        currNode = this.findLast();
        while(!(currNode.previous ==null))
        {
            console.log(currNode.element);
            currNode = currNode.previous;
        }
    }

    //
    this.head = new Node("head");
    this.find = _find;
    this.insert = _insert;
    this.remove = _remove;
    this.display = _display;
    this.findPrevious = _findPrevious;
    this.findLast = _findLast;
    this.dispReverse = _dispReverse;

}


//
var cities = new Llist();
cities.insert("Conway", "head");
cities.insert("Russellville", "Conway");
cities.insert("Alma", "Russellville");
cities.insert("yuq", "Conway");


//
cities.display();
console.log(cities);
cities.remove('Conway');
console.log(cities);
</script>

列表spa

<script type="text/javascript">
    //列表描述
    function list()
    {
        //
        var _clear = function(){
            delete _this.dataStore;
            _this.dataStore = [];
            _this.listsSize = _this.pos = 0;
        }

        //查找
        var _find =function(element){
            for(var i=0; i<_this.listsSize; i++)
            {
                if(_this.dataStore[i] === element)
                {
                    _this.pos = i;
                    return i;
                    break;
                }
            }
            return -1;
        }

        //
        var _toString =function(){
            return _this.dataStore.toString();
        }

        //
        var _insert =function(element,after){
            var insertPos = _this.find(after);
            if(insertPos > -1)
            {
                _this.dataStore.splice(insertPos+1,0,element);
                ++_this.listsSize;
                return true;
            }
            return false;
        }


        var _append =function(element){
            _this.dataStore[_this.listsSize++] = element;
            _this.pos++;

        }


        var _remove =function(element){
            var foundAt = _find(element);
            if(foundAt>-1)
            {
                _this.dataStore.splice(foundAt,1);
                --_this.listsSize;
                return false;
            }
        }


        var _front =function(){
            _this.pos=0;
        }


        var _end =function(){
            _this.pos = this.listsSize-1;
        }


        var _prev =function(){
            if(_this.pos > 0)
            {
                --this.pos;
            }
        }


        var _next =function(){
            if(_this.pos < _this.listsSize-1)
            {
                ++_this.pos;
            }
        }


        var _currPos =function(){
            return _this.pos;
        }


        var _moveTo =function(position){
            _this.pos = position;
        }


        var _getElement =function(){
            return _this.dataStore[_this.pos];
        }

        var _length =function(){
            return _this.listsSize;
        }

        var _contains =function(element){
            for(var i =0; i<_this.dataStore.length; ++i)
            {
                if(_this.dataStore[i] == element)
                {
                    return true;
                }
            }
            return false;
        }
        
        //屬性
        this.listsSize = 0;  //個數
        this.pos = 0;         //當前位置
        this.dataStore = []; //數據保存

        //方法
        this.clear = _clear;  //清空列表元素
        this.find = _find;    //查找
        this.toString = _toString; //返回列表字符串形式
        this.insert = _insert;  //現有元素後插入新元素
        this.append = _append;  //在列表的未尾插入新元素
        this.remove = _remove;  //從列表中刪除元素
        this.front = _front;       //將列表的當前位置設移動到第一個元素
        this.end = _end;//將列表的當前位置設移動到最後元素
        this.prev = _prev;//將當前位置前置一位
        this.next = _next;//將當前位置後置一位
        this.currPos = _currPos;//返回當前位置
        this.moveTo = _moveTo; //當前位置移動到指定位置
        this.getElement = _getElement; //返回當前位置的元素
        this.length = _length; //返回列表中元素個數
        this.contaics = _contains; //判斷值是否在列表中
        //
        var _this = this;
    }

    //
    var names = new list();

    //
    names.append("Zcl");
    names.append("Yyj");
    names.append("Yuq");
    names.append("Wpq");
    names.append("Fjb");
    names.append("Chj");

    //
    names.front();
    names.next();
    console.log(names);

    //
    names.next();
    names.next();
    names.prev();
    console.log(names);

    //
    console.log(names.find('Wpq'));
    //

    //names.clear();
    console.log(names);
    names.insert('Insert','Wpq');
    console.log(names);
    names.remove('Wpq');
    console.log(names);
    names.end();
    console.log(names);
    console.log(names.currPos());
    console.log(names.length());
    console.log(names.contaics('Insert'));
</script>

二叉樹,遞歸遍歷:.net

<script type="text/javascript">

/**
* 二叉查找樹是一種特殊的二叉樹,相對較小的值保存在左節點中,較大的值保存在右節點中。
* 這一特性使得查找的效率很高。
*/

// http://blog.csdn.net/sjf0115/article/details/8645991

//二叉樹
function Tree() {

    //節點
    var Node = function(data,left,right)
    {
        var _show = function()
        {
            return this.data;
        }
        //
        this.data = data;
        this.left = left;
        this.right = right;
        this.show = _show;
    }


    //添加節點
    var _insert = function(data)
    {
        var n = new Node(data,null,null);
        if(this.root == null)
        {
            this.root = n;
        }
        else
        {
            var current = this.root;
            var parent;
            //
            while(true)
            {
                parent = current;
                //
                if(data < current.data)
                {
                    current = current.left;
                    if(current == null)
                    {
                        parent.left = n;
                        break;
                    }
                }
                else
                {
                    current = current.right;
                    if(current==null)
                    {
                        parent.right = n;
                        break;
                    }
                }
            }
        }
    }


    /*
    前序遍歷:根節點->左子樹->右子樹
    中序遍歷:左子樹->根節點->右子樹
    後序遍歷:左子樹->右子樹->根節點
    */


    //中序遍歷
    var _inOrder = function(node)
    {
        if(!(node == null))
        {
            _inOrder(node.left);
            _this.showData += node.show()+',';
            _inOrder(node.right);
        }
    }


    //先序遍歷
    var _proOrder = function(node)
    {
        if(!(node == null))
        {
            _this.showData += node.show()+',';
            _proOrder(node.left);
            _proOrder(node.right);
        }
    }


    //後序遍歷
    var _postOrder = function(node)
    {
        if(!(node == null))
        {
            _postOrder(node.left);
            _postOrder(node.right);
            _this.showData += node.show()+',';
        }
    }



    //先序遍歷(非遞歸)
    var _proOrder2 = function(node)
    {
        //模擬棧
        var arr = [];
        //p是遍歷指針
        var p = node;
        //棧不空或者p不空時循環
        while( p!=null || arr.length !=0)
        {
            if(p != null)
            {
                //存入棧中  
                arr.push(p);
                _this.showData += p.show()+',';
                //遍歷左子樹 
                p = p.left;
            }
            else
            {
                p = arr.shift();
                p = p.right; 
            }
        }
    }



    //中序遍歷(非遞歸)
    var _inOrder2 = function(node)
    {
        //
        if(node == null)
        {
            return;
        }

        //
        var stack = []; //建立一個空棧 
        var node_pop;   //用來保存出棧節點  
        var pCur = node;       //定義指向當前訪問的節點的指針
        
        //
        while(pCur!=null || stack.length!=0)
        {
            if(pCur && pCur.left != null)
            {
                // stack.push(pCur);
                pCur = pCur.left;
                console.log(pCur);
            }
            else
            {
                // pCur && console.log(pCur.data);
                if(pCur)
                {
                    pCur = pCur.right;
                }
                //
                while( pCur == null && stack.length!=0)  
                {  
                    pCur = stack['0'];
                    console.log(pCur.data);
                    // pop_stack(stack,&node_pop);
                    pCur = pCur.right;
                }
                pCur = null;
                //
            }
        }
    }

    //後序遍歷(非遞歸)
    var _postOrder2 = function(node)
    {
        
        //
        if(node == null)
        {
            return;
        }

        //棧節點結構
        var snode =  function(node,num)
        {
            this.p = node; //p二叉樹節點結構,
            this.rvisited = num ? 1 :0 ; //rvisited:1表明p所指向的節點的右節點已被訪問過
        }

        //
        var stack = [];
        var p =node;

        //
        while( p!= null)
        {
            if(p != null)
            {
                var _node = new snode(p);
                stack.push(_node);
            }
            p = p.left;
        }

        //
        while(!stack.length == 0)
        {
            var sn = stack['0']; 
            //若其右孩子已經被訪問過,或是該元素沒有右孩子.
            if(sn.p.right == null || sn.rvisited)
            {
                p = stack.shift();
                // console.log(p.p.data);
                _this.showData += p.p.show()+',';
            }
            //若它的右孩子存在且rvisited爲0,說明之前尚未動過它的右孩子,因而就去處理一下其右孩子。
            else
            {
                sn.rvisited = 1;
                sn.p ? (p = sn.p.right) : p = null;
                //
                while(p != null)
                {
                    var _node = new snode(p);
                    stack.push(_node);
                    p = p.left;
                }
            }
        }
    }

    



    //分層遍歷,(按層次從上往下,從左往右[從右往左])
    var _layerOrder = function(node)
    {
        var arr = [];
        arr.push(node);
        while(arr.length)
        {
           var pNode = arr.pop();
            _this.showData += pNode.show()+',';
           //
           if(pNode.right != null)
           {
              arr.push(pNode.right);
           }
           if(pNode.left != null)
           {
              arr.push(pNode.left);
           }
        }
    }



    //查找最小值
    var _getMin = function()
    {
        var current = this.root;
        while(!(current.left ==null))
        {
            current = current.left;
        }
        return current.data;
    }


    //查找最大值
    var _getMax = function()
    {
        var current = this.root;
        while(!(current.right ==null))
        {
            current = current.right;
        }
        return current.data;
    }


    //查找
    var _find = function(data){
        var current = this.root;
        while(current != null)
        {
            if(current.data == data)
            {
                return current;
            }
            else if(data<current.data)
            {
                current = current.left;
            }
            else
            {
                 current = current.right;
            }
        }
    }

    
    //刪除
    var _remove = function(data)
    {
        this.root = this.removeNode(this.root,data);
    }


    //刪除節點
    var _removeNode = function(node,data)
    {
        if(node == null)
        {
            return null;
        }
        //
        if(data == node.data)
        {
            //沒有子節點的節點
            if(node.left == null && node.right ==null)
            {
                return null;
            }
            //沒有左子節點的節點
            if(node.left == null)
            {
                return node.right;
            }
            //沒有右子節點的節點
            if(node.right == null)
            {
                return node.left;
            }
            //有兩個子節點的節點
            var tempNode = this.getSmallest(node.right);
            //
            node.data = tempNode.data;
            node.right = this.removeNode(node.right,tempNode.data);
            return node;
        }
        else if(data < node.data)
        {
            node.left = this.removeNode(node.left,data);
            return node;
        }
        else
        {
            node.right = this.removeNode(node.right,data);
            return node;
        }
    }



    //查找左邊最小節點
    var _getSmallest = function(treeNode)
    {
        var current = treeNode;
        while(!(current.left ==null))
        {
            current = current.left;
        }
        return current;
    }


    //
    this.root = null;

    //添加
    this.insert = _insert;

    //遞歸遍歷
    this.inOrder = _inOrder;
    this.proOrder = _proOrder;
    this.postOrder = _postOrder;
    this.layerOrder =_layerOrder;

    //非遞歸遍歷
    this.proOrder2 = _proOrder2;
    this.postOrder2 = _postOrder2;
    this.inOrder2 = _inOrder2;

    //查找
    this.getMin = _getMin;
    this.getMax = _getMax;
    this.find = _find;

    //刪除
    this.remove = _remove;
    this.removeNode =_removeNode;
    this.getSmallest = _getSmallest;

    //
    this.showData = '';
    var _this = this;
}

//
var nums = new Tree();
//
nums.insert(56);
nums.insert(22);
nums.insert(81);
nums.insert(10);
nums.insert(30);
nums.insert(77);
nums.insert(92);
//
nums.insert(6);
nums.insert(11);
nums.insert(29);
nums.insert(32);
nums.insert(76);
nums.insert(78);
nums.insert(89);
nums.insert(94);
/* */
//
console.log(nums);
//
console.log("中序遍歷:");
nums.inOrder(nums.root);
console.log(nums);
//
console.log("先序遍歷:");
nums.showData = [];
nums.proOrder(nums.root);
console.log(nums);
//
console.log("後序遍歷:");
nums.showData = [];
nums.postOrder(nums.root);
console.log(nums);
//
console.log("分層遍歷:");
nums.showData = [];
nums.layerOrder(nums.root);
console.log(nums);

//
console.log("先序遍歷(非遞歸):");
nums.showData = [];
nums.proOrder2(nums.root);
console.log(nums);


//
console.log("後序遍歷(非遞歸):");
nums.showData = [];
nums.postOrder2(nums.root);
console.log(nums);


//
console.log("中序遍歷(非遞歸):");
nums.showData = [];
nums.inOrder2(nums.root);
console.log(nums);


//
console.log("最小值:");
console.log(nums.getMin());
console.log("最大值:");
console.log(nums.getMax());

//
console.log("查找:92");
console.log(nums.find('92'));

//
console.log("刪除節點:81");  
nums.remove('81');

//
console.log("中序遍歷:");
nums.showData = [];
nums.inOrder(nums.root);
console.log(nums);
//

</script>

二叉樹,非遞歸遍歷:指針

<script type="text/javascript">

/**
* 二叉查找樹是一種特殊的二叉樹,相對較小的值保存在左節點中,較大的值保存在右節點中。
* 這一特性使得查找的效率很高。
*/

// http://blog.csdn.net/sjf0115/article/details/8645991

//二叉樹
function Tree() {

    //節點
    var Node = function(data,left,right)
    {
        var _show = function()
        {
            return this.data;
        }
        //
        this.data = data;
        this.left = left;
        this.right = right;
        this.show = _show;
    }


    //添加節點
    var _insert = function(data)
    {
        var n = new Node(data,null,null);
        if(this.root == null)
        {
            this.root = n;
        }
        else
        {
            var current = this.root;
            var parent;
            //
            while(true)
            {
                parent = current;
                //
                if(data < current.data)
                {
                    current = current.left;
                    if(current == null)
                    {
                        parent.left = n;
                        break;
                    }
                }
                else
                {
                    current = current.right;
                    if(current==null)
                    {
                        parent.right = n;
                        break;
                    }
                }
            }
        }
    }


    /*
    前序遍歷:根節點->左子樹->右子樹
    中序遍歷:左子樹->根節點->右子樹
    後序遍歷:左子樹->右子樹->根節點
    */

    //分層遍歷,(按層次從上往下,從左往右[從右往左])
    var _layerOrder = function(node)
    {
        var arr = [];
        arr.push(node);
        while(arr.length)
        {
           var pNode = arr.pop();
            _this.showData += pNode.show()+',';
           //
           if(pNode.right != null)
           {
              arr.push(pNode.right);
           }
           if(pNode.left != null)
           {
              arr.push(pNode.left);
           }
        }
    }

    //先序遍歷(非遞歸)
    var _proOrder2 = function(node)
    {
        //模擬棧
        var arr = [];
        //p是遍歷指針
        var p = node;
        //棧不空或者p不空時循環
        while( p!=null || arr.length !=0)
        {
            if(p != null)
            {
                //存入棧中  
                arr.push(p);
                _this.showData += p.show()+',';
                //遍歷左子樹 
                p = p.left;
            }
            else
            {
                p = arr.shift();
                p = p.right; 
            }
        }
    }


    //中序遍歷(非遞歸)
    var _inOrder2 = function(node)
    {

    }





    //後序遍歷(非遞歸)
    var _postOrder2 = function(node)
    {
        
        //棧節點結構
        var snode =  function(node,num)
        {
            this.p = node; //p二叉樹節點結構,
            this.rvisited = num ? 1 :0 ; //rvisited:1表明p所指向的節點的右節點已被訪問過
        }

        //
        var stack = [];
        var p =node;

        //
        while( p!= null)
        {
            
            var _node = new snode(p);
            stack.push(_node);
            p = p.left;
        }

        //
        while(!stack.length == 0)
        {
            var sn = stack['0']; 
            //若其右孩子已經被訪問過,或是該元素沒有右孩子.
            if(sn.p.right == null || sn.rvisited)
            {
                p = stack.shift(); //出棧
                _this.showData += p.p.show()+',';
            }
            //若它的右孩子存在且rvisited爲0,說明之前尚未動過它的右孩子,因而就去處理一下其右孩子。
            else
            {
                sn.rvisited = 1;
                sn.p ? (p = sn.p.right) : p = null;
                //
                while(p != null)
                {
                    var _node = new snode(p);
                    stack.push(_node);
                    p = p.left;
                }
            }
        }
    }



    //
    this.root = null;

    //添加
    this.insert = _insert;


    //非遞歸遍歷
    this.proOrder2 = _proOrder2;
    this.postOrder2 = _postOrder2;
    this.inOrder2 = _inOrder2;
    this.layerOrder = _layerOrder;



    //
    this.showData = '';
    var _this = this;
}

//
var nums = new Tree();
//
nums.insert(56);
nums.insert(22);
nums.insert(81);
nums.insert(10);
nums.insert(30);
nums.insert(77);
nums.insert(92);
//
nums.insert(6);
nums.insert(11);
nums.insert(29);
nums.insert(32);
nums.insert(76);
nums.insert(78);
nums.insert(89);
nums.insert(94);
/* */


//
console.log("分層遍歷(非遞歸):");
nums.showData = [];
nums.layerOrder(nums.root);
console.log(nums);


//
console.log("先序遍歷(非遞歸):");
nums.showData = [];
nums.proOrder2(nums.root);
console.log(nums);


//
console.log("後序遍歷(非遞歸):");
nums.showData = [];
nums.postOrder2(nums.root);
console.log(nums);


//
console.log("中序遍歷(非遞歸):");
nums.showData = [];
nums.inOrder2(nums.root);
console.log(nums);


//

</script>
相關文章
相關標籤/搜索