JS二叉樹的簡單實現方法樣本【轉載】

來源:互聯網
上載者:User

標籤:root   儲存   class   二叉樹實現   turn   方法   簡單的   javascrip   desc   

簡單的二叉樹實現,並且實現升序和降序排序輸出

function Node(data , left,right){  this.data = data;  this.left = left;  this.right = right;  this.show = show;  function show(){    return this.data;  }};function Bst(){  this.root = null;  this.insert = insert;//插入  this.inOrder = inOrder;//中序遍曆(升序)  this.inOrderDesc = inOrderDesc;//中序遍曆(降序)  this.preOrder = preOrder;//先序遍曆  this.postOrder = postOrder;//後續遍曆  this.getMin = getMin;//最大值  this.getMax = getMax;//最小值  this.find = find;//尋找值  this.remove = remove;//刪除節點  this.count = count;//擷取節點數量  function insert(data){    //建立一個新的節點    var newNode = new Node(data,null,null);    //判斷是否存在根節點,沒有將新節點存入    if(this.root == null){      this.root = newNode;    }else{      //擷取根節點      var current = this.root;      var parent;      while(true){        //將當前節點儲存為父節點        parent = current;        //將小的資料放在左節點        if(data < current.data){          //擷取當前節點的左節點          //判斷當前節點下的左節點是否有資料          current = current.left;          if(current == null){            //如果沒有資料將新節點存入當前節點下的左節點            parent.left = newNode;            break;          }        }else{          current = current.right;          if(current == null){            parent.right = newNode;            break;          }        }      }    }  }  function inOrder(node){    var data = [];    _inOrder(node,data);    return data;  }  function inOrderDesc(node){    var data = [];    _inOrderDesc(node,data);    return data;  }  function preOrder(node){    var data = [];    _preOrder(node,data);    return data;  }  function postOrder(node){    var data = [];    _postOrder(node,data);    return data;  }  function _inOrder(node,data){    if(!(node == null)){      _inOrder(node.left,data);      data.push(node.show());      _inOrder(node.right,data);    }  }  function _inOrderDesc(node,data){    debugger;    if(!(node == null)){      _inOrderDesc(node.right,data);      data.push(node.show());      _inOrderDesc(node.left,data);    }  }  function _preOrder(node,data){    if(!(node == null)){      data.push(node.show());      _preOrder(node.left,data);      _preOrder(node.right,data);    }  }  function _postOrder(node,data){    if(!(node == null)){      _postOrder(node.left,data);      _postOrder(node.right,data);      data.push(node.show());    }  }  function getMin(){    var current = this.root;    while(!(current.left == null)){    current = current.left;    }    return current.data;  }  function getMax(){    var current = this.root;    while(!(current.right == null)){    current = current.right;    }    return current.data;  }  function find(data){    var current = this.root;    while(current != null){      if(data == current.data){        return current;      }else if(data < current.data){        current = current.left;      }else{        current = current.right;      }    }    return null;  }  function getSmallest(node){    var current = node;    while(!(current.left == null)){      current = current.left;    }    return current;  }  function remove(data){    root = removeNode(this.root,data);  }  function removeNode(node,data){    if(node == null){      return null;    }    if(data == node.data){      //如果沒有只節點      if(node.left == null && node.right){        return null;      }      //如果沒有左節點      if(node.left == null){        return node.right;      }      //如果沒有右節點      if(node.right == null){        return node.left;      }      //有兩節點      var tempNode = getSmallest(node.right);      node.data = tempNode.data;      node.right = removeNode(node.right,tempNode.data);      return node;    }else if(data < node.data){      node.left = removeNode(node.left,data);      return node;    }else{      node.right = removeNode(node.right,data);      return node;    }  }  function count(){    var counts = 0;    var current = this.root;    if(current == null){      return counts;    }    return _count(current,counts);  }  function _count(node,counts){    debugger;    if(!(node == null)){      counts ++;      counts = _count(node.left,counts);;      counts = _count(node.right,counts);    }    return counts;  }}

  

function Node(data , left,right){  this.data = data;  this.left = left;  this.right = right;  this.show = show;  function show(){    return this.data;  }};function Bst(){  this.root = null;  this.insert = insert;//插入  this.inOrder = inOrder;//中序遍曆(升序)  this.inOrderDesc = inOrderDesc;//中序遍曆(降序)  this.preOrder = preOrder;//先序遍曆  this.postOrder = postOrder;//後續遍曆  this.getMin = getMin;//最大值  this.getMax = getMax;//最小值  this.find = find;//尋找值  this.remove = remove;//刪除節點  this.count = count;//擷取節點數量  function insert(data){    //建立一個新的節點    var newNode = new Node(data,null,null);    //判斷是否存在根節點,沒有將新節點存入    if(this.root == null){      this.root = newNode;    }else{      //擷取根節點      var current = this.root;      var parent;      while(true){        //將當前節點儲存為父節點        parent = current;        //將小的資料放在左節點        if(data < current.data){          //擷取當前節點的左節點          //判斷當前節點下的左節點是否有資料          current = current.left;          if(current == null){            //如果沒有資料將新節點存入當前節點下的左節點            parent.left = newNode;            break;          }        }else{          current = current.right;          if(current == null){            parent.right = newNode;            break;          }        }      }    }  }  function inOrder(node){    var data = [];    _inOrder(node,data);    return data;  }  function inOrderDesc(node){    var data = [];    _inOrderDesc(node,data);    return data;  }  function preOrder(node){    var data = [];    _preOrder(node,data);    return data;  }  function postOrder(node){    var data = [];    _postOrder(node,data);    return data;  }  function _inOrder(node,data){    if(!(node == null)){      _inOrder(node.left,data);      data.push(node.show());      _inOrder(node.right,data);    }  }  function _inOrderDesc(node,data){    debugger;    if(!(node == null)){      _inOrderDesc(node.right,data);      data.push(node.show());      _inOrderDesc(node.left,data);    }  }  function _preOrder(node,data){    if(!(node == null)){      data.push(node.show());      _preOrder(node.left,data);      _preOrder(node.right,data);    }  }  function _postOrder(node,data){    if(!(node == null)){      _postOrder(node.left,data);      _postOrder(node.right,data);      data.push(node.show());    }  }  function getMin(){    var current = this.root;    while(!(current.left == null)){    current = current.left;    }    return current.data;  }  function getMax(){    var current = this.root;    while(!(current.right == null)){    current = current.right;    }    return current.data;  }  function find(data){    var current = this.root;    while(current != null){      if(data == current.data){        return current;      }else if(data < current.data){        current = current.left;      }else{        current = current.right;      }    }    return null;  }  function getSmallest(node){    var current = node;    while(!(current.left == null)){      current = current.left;    }    return current;  }  function remove(data){    root = removeNode(this.root,data);  }  function removeNode(node,data){    if(node == null){      return null;    }    if(data == node.data){      //如果沒有只節點      if(node.left == null && node.right){        return null;      }      //如果沒有左節點      if(node.left == null){        return node.right;      }      //如果沒有右節點      if(node.right == null){        return node.left;      }      //有兩節點      var tempNode = getSmallest(node.right);      node.data = tempNode.data;      node.right = removeNode(node.right,tempNode.data);      return node;    }else if(data < node.data){      node.left = removeNode(node.left,data);      return node;    }else{      node.right = removeNode(node.right,data);      return node;    }  }  function count(){    var counts = 0;    var current = this.root;    if(current == null){      return counts;    }    return _count(current,counts);  }  function _count(node,counts){    debugger;    if(!(node == null)){      counts ++;      counts = _count(node.left,counts);;      counts = _count(node.right,counts);    }    return counts;  }}

 

JS二叉樹的簡單實現方法樣本【轉載】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.