最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript數(shù)據結構與算法之二叉樹遍歷算法詳解【先序、中序、后序】

 更新時間:2019年02月21日 14:17:06   作者:白楊-M  
這篇文章主要介紹了JavaScript數(shù)據結構與算法之二叉樹遍歷算法,結合實例形式詳細分析了javascript二叉樹的定義及先序遍歷、中序遍歷、后序遍歷等相關遍歷操作實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了JavaScript數(shù)據結構與算法之二叉樹遍歷算法。分享給大家供大家參考,具體如下:

javascript數(shù)據結構與算法--二叉樹遍歷(先序)

先序遍歷先訪問根節(jié)點, 然后以同樣方式訪問左子樹和右子樹

代碼如下:

/*
 *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中
 *
 *
 * */
/*用來生成一個節(jié)點*/
function Node(data, left, right) {
  this.data = data;//節(jié)點存儲的數(shù)據
  this.left = left;
  this.right = right;
  this.show = show;
}
function show() {
  return this.data;
}
/*用來生成一個二叉樹*/
function BST() {
  this.root = null;
  this.insert = insert;
}
/*將數(shù)據插入二叉樹
 (1)設根節(jié)點為當前節(jié)點。
 (2)如果待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點;反
 之,執(zhí)行第4步。
 (3)如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 (4)設新的當前節(jié)點為原節(jié)點的右節(jié)點。
 (5)如果當前節(jié)點的右節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 * */
function insert(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;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {//如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。
          parent.left = n;
          break;
        }
      }
      else {
        current = current.right;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {
          parent.right = n;
          break;
        }
      }
    }
  }
}
/*先序遍歷
 *用遞歸的方法
 */
function preOrder(node) {
  if (!(node == null)) {
    console.log(node.show() + " ");
    preOrder(node.left);
    preOrder(node.right);
  }
}
/* 測試代碼 */
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
console.log("先序遍歷: ");
preOrder(nums.root);

運行結果:

javascript數(shù)據結構與算法--二叉樹遍歷(中序)

中序遍歷按照節(jié)點上的鍵值,以升序訪問BST上的所有節(jié)點

代碼如下:

/*
 *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中
 *
 *
 * */
/*用來生成一個節(jié)點*/
function Node(data, left, right) {
  this.data = data;//節(jié)點存儲的數(shù)據
  this.left = left;
  this.right = right;
  this.show = show;
}
function show() {
  return this.data;
}
/*用來生成一個二叉樹*/
function BST() {
  this.root = null;
  this.insert = insert;
}
/*將數(shù)據插入二叉樹
 (1)設根節(jié)點為當前節(jié)點。
 (2)如果待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點;反
 之,執(zhí)行第4步。
 (3)如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 (4)設新的當前節(jié)點為原節(jié)點的右節(jié)點。
 (5)如果當前節(jié)點的右節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 * */
function insert(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;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {//如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。
          parent.left = n;
          break;
        }
      }
      else {
        current = current.right;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {
          parent.right = n;
          break;
        }
      }
    }
  }
}
/*中序遍歷
*用遞歸的方法
*/
function inOrder(node) {
  if (!(node == null)) {
    inOrder(node.left);
    console.log(node.show() + " ");
    inOrder(node.right);
  }
}
/* 測試代碼 */
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
console.log("中序遍歷: ");
inOrder(nums.root);

運行結果:

javascript數(shù)據結構與算法--二叉樹遍歷(后序)

后序遍歷先訪問葉子節(jié)點,從左子樹到右子樹,再到根節(jié)點。

/*
 *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中
 *
 *
 * */
/*用來生成一個節(jié)點*/
function Node(data, left, right) {
  this.data = data;//節(jié)點存儲的數(shù)據
  this.left = left;
  this.right = right;
  this.show = show;
}
function show() {
  return this.data;
}
/*用來生成一個二叉樹*/
function BST() {
  this.root = null;
  this.insert = insert;
}
/*將數(shù)據插入二叉樹
 (1)設根節(jié)點為當前節(jié)點。
 (2)如果待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點;反
 之,執(zhí)行第4步。
 (3)如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 (4)設新的當前節(jié)點為原節(jié)點的右節(jié)點。
 (5)如果當前節(jié)點的右節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)
 執(zhí)行下一次循環(huán)。
 * */
function insert(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;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {//如果當前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。
          parent.left = n;
          break;
        }
      }
      else {
        current = current.right;//待插入節(jié)點保存的數(shù)據小于當前節(jié)點,則設新的當前節(jié)點為原節(jié)點的左節(jié)點
        if (current == null) {
          parent.right = n;
          break;
        }
      }
    }
  }
}
/*后序遍歷
 *用遞歸的方法
 */
function postOrder(node) {
  if (!(node == null)) {
    postOrder(node.left);
    postOrder(node.right);
    console.log(node.show() + " ");
  }
}
/* 測試代碼 */
var nums = new BST();
nums.insert(23);
nums.insert(45);
nums.insert(16);
nums.insert(37);
nums.insert(3);
nums.insert(99);
nums.insert(22);
console.log("后序遍歷: ");
postOrder(nums.root);

運行結果:

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。

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

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

相關文章

最新評論

荥经县| 临朐县| 古交市| 绥棱县| 仪陇县| 辉县市| 安溪县| 东山县| 阳谷县| 菏泽市| 嵊州市| 新竹县| 松滋市| 陵川县| 瑞丽市| 永安市| 多伦县| 金川县| 海伦市| 万山特区| 板桥市| 彭州市| 海门市| 合水县| 平湖市| 南江县| 澳门| 高安市| 马鞍山市| 青浦区| 望谟县| 康保县| 龙州县| 墨脱县| 望江县| 高要市| 云南省| 迁西县| 东台市| 长兴县| 泰和县|