国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS二叉樹的簡單實現方法示例

2019-11-19 16:55:34
字體:
來源:轉載
供稿:網友

本文實例講述了JS二叉樹的簡單實現方法。分享給大家供大家參考,具體如下:

今天學習了一下 二叉樹的實現,在此記錄一下

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

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;  }}

更多關于JavaScript相關內容感興趣的讀者可查看本站專題:《JavaScript數據結構與算法技巧總結》、《JavaScript數學運算用法總結》、《JavaScript排序算法總結》、《JavaScript遍歷算法與技巧總結》、《JavaScript查找算法技巧總結》及《JavaScript錯誤與調試技巧總結

希望本文所述對大家JavaScript程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临泉县| 荆州市| 贺兰县| 昆山市| 汉川市| 武鸣县| 宁化县| 基隆市| 龙川县| 正镶白旗| 大新县| 荣昌县| 遵义市| 隆回县| 中西区| 灵璧县| 陕西省| 石渠县| 库尔勒市| 晴隆县| 青海省| 门头沟区| 那坡县| 抚州市| 邓州市| 吉隆县| 阳泉市| 泰州市| 富民县| 宁乡县| 墨竹工卡县| 奇台县| 东丰县| 二连浩特市| 犍为县| 英德市| 九寨沟县| 抚顺县| 黄骅市| 孙吴县| 嵊泗县|