亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

js遍歷二叉樹和多叉樹結(jié)構(gòu)

junbaor / 1779人閱讀

摘要:二叉樹的層級遍歷創(chuàng)建一個(gè)二叉樹輸出函數(shù)先訪問左子樹,再訪問自身,再訪問右子樹先訪問自身,再訪問左子樹,再訪問右子樹先訪問左子樹,再訪問右子樹再訪問自身層級遍歷多叉樹的層級遍歷創(chuàng)建一個(gè)多叉樹輸出函數(shù)遞歸遍歷每個(gè)節(jié)點(diǎn)方法方法方法層級遍歷每

1、二叉樹的層級遍歷

創(chuàng)建一個(gè)二叉樹

class Binary{
  constructor(data,left,right){
  this.data = data
  this.left = left
  this.right = right
 }
}

輸出函數(shù)

function Output(){
  const left = new Binary(1, new Binary(2),new Binary(3))
  const right = new Binary(4,new Binary(5),new Binary(6))
  const root = new Binary(0,left,right)
//console.log(root) 
/*
       0
     /  
    1    4
   /   / 
  2  3  5  6
 */

 inOrder(root) // 2 1 3 0 5 4 6
 preOrder(root) //0 1 2 3 4 5 6
 postOrder(root) //2,3,1,5,6,4,0
 levelOrder(root) // 0,1,4,2,3,5,6
}()

先訪問左子樹,再訪問自身,再訪問右子樹

function inOrder(root){
 if(root){
   inOrder(root.left)
   console.log(root.data)
   inOrder(root.right)
 }
}

先訪問自身,再訪問左子樹,再訪問右子樹

function preOrder(root){
 if(root){
  console.log(root.data)
  preOrder(root.left)
  preOrder(root.right)
 }
}

先訪問左子樹,再訪問右子樹再訪問自身

function postOrder(root){
 if(root){
   postOrder(root.left)
   postOrder(root.right)
   console.log(root.data)
 }
}

層級遍歷

function levelOrder(root){
   var queue = []
   queue.unshift(root)
   while(queue.length){
     var current = queue.pop()
     console.log(current.data)
     if(current.left){
       queue.unshift(current.left)
     }
     if(current.right){
       queue.unshift(current.right)
     }
   }
}
2、多叉樹的層級遍歷

創(chuàng)建一個(gè)多叉樹

class TreeNode {
  constructor(data){
    this.data = data
    this.children = []
  }
}

輸出函數(shù)

function main(){
 const root = new TreeNode(0)
 const node2 = new TreeNode(2)
 cosnt node2.children.push(new TreeNode(7))
 const node2.chilfren.push(new TreeNode(8))
 const node2.children.push(new TreeNode(9))
 const node4 = new TreeNode(4)
 const node3 = new TreeNode(3)
 const node3.children.push(new TreeNode(6))
 const node3.children.push(new TreeNode(5))
 root.children.push(node2)
 root.children.push(node4)
 root.children.push(node3)
//console.log(root)
/*
                        0
                   /    |    
                  2     4     3
               /  |         /  
              7   8   9     6    5  
*/
traverse1(root)
traverse2(traverse2[root])
var result = []
traverse3([root],result)
console.log(result)
levelOrder(root)
}()
遞歸遍歷每個(gè)節(jié)點(diǎn)

方法1

function traverse1(node){
 if(!node){
  return []
 }
var result = []
result.push(node.data)
if(node.children){
for(var i = 0;i<=node.children.length-1;i++){
  result = result.concat(traverse1(node.children[i]))
}
 return result
}
}

方法2

function tranverse2(nodeList){
if(!nodeList){
return []
}
var result = []
for(var i=0;i<=nodeList.length-1;i++){
 result.push(nodeList[i].data)
  if(nodeList[i].children){
    result = result.concat(traverse2(nodeList[i].children))
  }
 }
return result
}

方法3

function traverse3(nodeList,result){
  if(!nodeList){
    return false
  }
 for(var i=0;i<=nodeList.length-1;i++){
  resule.push(nodeList[i].data)
  if(nodeList[i].childern){
    traverse3(nodeList[i].children,result)
  }
 }
}
層級遍歷每個(gè)節(jié)點(diǎn)
funciton levelOrder(root){
 var queue = []
 queue.unshift(root)
var result = []
while(quue.length){
 var current  = queue.pop()
 result.push(current.data)
 for(var i =0;i<=current.children.length-1;i++){
   if(current.children[i]){
    queue.unshift(current.children[i])
    }
  }
 }
console.log(result)
}

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/109904.html

相關(guān)文章

  • 樹及其外部存儲(chǔ)

    摘要:切記,紅黑樹在旋轉(zhuǎn)和顏色變換的過程中,必須遵守紅黑樹的幾條規(guī)則。樹的外部存儲(chǔ)磁盤布局計(jì)算機(jī)中的機(jī)械磁盤是由磁頭和圓盤組成,每個(gè)圓盤上劃分為多個(gè)磁道,每個(gè)磁道又劃分為多個(gè)扇區(qū)。 術(shù)語 showImg(https://segmentfault.com/img/bVbai3r?w=643&h=407); 根 ????樹最頂端的節(jié)點(diǎn)稱為根,一棵樹只有一個(gè)根 父節(jié)點(diǎn) ????每個(gè)節(jié)...

    _Dreams 評論0 收藏0
  • 一篇文章學(xué)會(huì)二叉樹和二叉查找樹

    摘要:二叉樹和二叉查找樹一個(gè)父節(jié)點(diǎn)的兩個(gè)子節(jié)點(diǎn)分別稱為左節(jié)點(diǎn)和右節(jié)點(diǎn)。下圖展示了一顆二叉樹當(dāng)考慮某種特殊的二叉樹,比如二叉查找樹時(shí),確定子節(jié)點(diǎn)非常重要。實(shí)現(xiàn)二叉查找樹定義對象。現(xiàn)在可以創(chuàng)建一個(gè)類來表示二叉查找樹。因此二叉查找樹也被叫做二叉排序樹。 樹是計(jì)算機(jī)科學(xué)中經(jīng)常用到的一種數(shù)據(jù)結(jié)構(gòu)。樹是一種非線性的數(shù)據(jù)結(jié)構(gòu),以分層的方式存儲(chǔ)數(shù)據(jù)。 樹被用來存儲(chǔ)具有層級關(guān)系的數(shù)據(jù),比如文件系統(tǒng)中的文件。 ...

    BaronZhang 評論0 收藏0
  • 【從蛋殼到滿天飛】JAVA 數(shù)據(jù)結(jié)構(gòu)解析和算法實(shí)現(xiàn)-二分搜索樹

    摘要:在數(shù)據(jù)結(jié)構(gòu)領(lǐng)域?qū)?yīng)樹結(jié)構(gòu)來說二叉樹是最常用的一種樹結(jié)構(gòu),二叉樹具有一個(gè)唯一的根節(jié)點(diǎn),也就是最上面的節(jié)點(diǎn)。二叉樹每個(gè)節(jié)點(diǎn)最多有兩個(gè)孩子,一個(gè)孩子都沒有的節(jié)點(diǎn)通常稱之為葉子節(jié)點(diǎn),二叉樹每個(gè)節(jié)點(diǎn)最多有一個(gè)父親,根節(jié)點(diǎn)是沒有父親節(jié)點(diǎn)的。 showImg(https://segmentfault.com/img/remote/1460000018597053?w=1832&h=9943); 前言...

    ghnor 評論0 收藏0
  • 【從蛋殼到滿天飛】JAVA 數(shù)據(jù)結(jié)構(gòu)解析和算法實(shí)現(xiàn)-二分搜索樹

    摘要:在數(shù)據(jù)結(jié)構(gòu)領(lǐng)域?qū)?yīng)樹結(jié)構(gòu)來說二叉樹是最常用的一種樹結(jié)構(gòu),二叉樹具有一個(gè)唯一的根節(jié)點(diǎn),也就是最上面的節(jié)點(diǎn)。二叉樹每個(gè)節(jié)點(diǎn)最多有兩個(gè)孩子,一個(gè)孩子都沒有的節(jié)點(diǎn)通常稱之為葉子節(jié)點(diǎn),二叉樹每個(gè)節(jié)點(diǎn)最多有一個(gè)父親,根節(jié)點(diǎn)是沒有父親節(jié)點(diǎn)的。 showImg(https://segmentfault.com/img/remote/1460000018597053?w=1832&h=9943); 前言...

    FuisonDesign 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<