這篇文章給大家介紹使用JavaScript怎么實(shí)現(xiàn)一個(gè)二叉樹,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè),為客戶提供成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)開發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
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;//后續(xù)遍歷
this.getMin = getMin;//最大值
this.getMax = getMax;//最小值
this.find = find;//查找值
this.remove = remove;//刪除節(jié)點(diǎn)
this.count = count;//獲取節(jié)點(diǎn)數(shù)量
function insert(data){
//創(chuàng)建一個(gè)新的節(jié)點(diǎn)
var newNode = new Node(data,null,null);
//判斷是否存在根節(jié)點(diǎn),沒有將新節(jié)點(diǎn)存入
if(this.root == null){
this.root = newNode;
}else{
//獲取根節(jié)點(diǎn)
var current = this.root;
var parent;
while(true){
//將當(dāng)前節(jié)點(diǎn)保存為父節(jié)點(diǎn)
parent = current;
//將小的數(shù)據(jù)放在左節(jié)點(diǎn)
if(data < current.data){
//獲取當(dāng)前節(jié)點(diǎn)的左節(jié)點(diǎn)
//判斷當(dāng)前節(jié)點(diǎn)下的左節(jié)點(diǎn)是否有數(shù)據(jù)
current = current.left;
if(current == null){
//如果沒有數(shù)據(jù)將新節(jié)點(diǎn)存入當(dāng)前節(jié)點(diǎn)下的左節(jié)點(diǎn)
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){
//如果沒有只節(jié)點(diǎn)
if(node.left == null && node.right){
return null;
}
//如果沒有左節(jié)點(diǎn)
if(node.left == null){
return node.right;
}
//如果沒有右節(jié)點(diǎn)
if(node.right == null){
return node.left;
}
//有兩節(jié)點(diǎn)
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;
}
}關(guān)于使用JavaScript怎么實(shí)現(xiàn)一個(gè)二叉樹就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
分享名稱:使用JavaScript怎么實(shí)現(xiàn)一個(gè)二叉樹
鏈接分享:http://www.jbt999.com/article38/pdpspp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、電子商務(wù)、微信小程序、企業(yè)建站、App設(shè)計(jì)、營(yíng)銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)