JavaScript數(shù)據結構與算法之二叉樹遍歷算法詳解【先序、中序、后序】
本文實例講述了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程序設計有所幫助。
相關文章
JavaScript實現(xiàn)點擊自動選擇TextArea文本的方法
這篇文章主要介紹了JavaScript實現(xiàn)點擊自動選擇TextArea文本的方法,涉及javascript中focus()、select()方法的使用技巧,非常簡單實用,需要的朋友可以參考下2015-07-07

