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

首頁 > 開發(fā) > JS > 正文

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

2024-05-06 16:48:20
字體:
供稿:網(wǎng)友

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

javascript數(shù)據(jù)結(jié)構(gòu)與算法--二叉樹遍歷(先序)

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

JavaScript,數(shù)據(jù)結(jié)構(gòu),算法,二叉,樹遍歷

代碼如下:

/* *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中 * * * *//*用來生成一個節(jié)點*/function Node(data, left, right) {  this.data = data;//節(jié)點存儲的數(shù)據(jù)  this.left = left;  this.right = right;  this.show = show;}function show() {  return this.data;}/*用來生成一個二叉樹*/function BST() {  this.root = null;  this.insert = insert;}/*將數(shù)據(jù)插入二叉樹 (1)設(shè)根節(jié)點為當(dāng)前節(jié)點。 (2)如果待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點;反 之,執(zhí)行第4步。 (3)如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù) 執(zhí)行下一次循環(huán)。 (4)設(shè)新的當(dāng)前節(jié)點為原節(jié)點的右節(jié)點。 (5)如果當(dāng)前節(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ù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點        if (current == null) {//如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。          parent.left = n;          break;        }      }      else {        current = current.right;//待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(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);

運行結(jié)果:

JavaScript,數(shù)據(jù)結(jié)構(gòu),算法,二叉,樹遍歷

javascript數(shù)據(jù)結(jié)構(gòu)與算法--二叉樹遍歷(中序)

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

JavaScript,數(shù)據(jù)結(jié)構(gòu),算法,二叉,樹遍歷

代碼如下:

/* *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中 * * * *//*用來生成一個節(jié)點*/function Node(data, left, right) {  this.data = data;//節(jié)點存儲的數(shù)據(jù)  this.left = left;  this.right = right;  this.show = show;}function show() {  return this.data;}/*用來生成一個二叉樹*/function BST() {  this.root = null;  this.insert = insert;}/*將數(shù)據(jù)插入二叉樹 (1)設(shè)根節(jié)點為當(dāng)前節(jié)點。 (2)如果待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點;反 之,執(zhí)行第4步。 (3)如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù) 執(zhí)行下一次循環(huán)。 (4)設(shè)新的當(dāng)前節(jié)點為原節(jié)點的右節(jié)點。 (5)如果當(dāng)前節(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ù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點        if (current == null) {//如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。          parent.left = n;          break;        }      }      else {        current = current.right;//待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(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);

運行結(jié)果:

JavaScript,數(shù)據(jù)結(jié)構(gòu),算法,二叉,樹遍歷

javascript數(shù)據(jù)結(jié)構(gòu)與算法--二叉樹遍歷(后序)

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

 

/* *二叉樹中,相對較小的值保存在左節(jié)點上,較大的值保存在右節(jié)點中 * * * *//*用來生成一個節(jié)點*/function Node(data, left, right) {  this.data = data;//節(jié)點存儲的數(shù)據(jù)  this.left = left;  this.right = right;  this.show = show;}function show() {  return this.data;}/*用來生成一個二叉樹*/function BST() {  this.root = null;  this.insert = insert;}/*將數(shù)據(jù)插入二叉樹 (1)設(shè)根節(jié)點為當(dāng)前節(jié)點。 (2)如果待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點;反 之,執(zhí)行第4步。 (3)如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù) 執(zhí)行下一次循環(huán)。 (4)設(shè)新的當(dāng)前節(jié)點為原節(jié)點的右節(jié)點。 (5)如果當(dāng)前節(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ù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(jié)點為原節(jié)點的左節(jié)點        if (current == null) {//如果當(dāng)前節(jié)點的左節(jié)點為null,就將新的節(jié)點插入這個位置,退出循環(huán);反之,繼續(xù)執(zhí)行下一次while循環(huán)。          parent.left = n;          break;        }      }      else {        current = current.right;//待插入節(jié)點保存的數(shù)據(jù)小于當(dāng)前節(jié)點,則設(shè)新的當(dāng)前節(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);

運行結(jié)果:

JavaScript,數(shù)據(jù)結(jié)構(gòu),算法,二叉,樹遍歷

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


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴彦淖尔市| 东兴市| 上栗县| 凉城县| 胶州市| 中牟县| 基隆市| 盘山县| 晴隆县| 库尔勒市| 光山县| 原平市| 南澳县| 东乡| 葵青区| 博客| 岳普湖县| 泰州市| 林芝县| 休宁县| 城市| 赤峰市| 龙陵县| 龙江县| 富民县| 墨玉县| 富源县| 汽车| 黑水县| 上高县| 毕节市| 沙河市| 常州市| 瓦房店市| 友谊县| 彭水| 汝南县| 海林市| 东兰县| 新乡县| 盐亭县|