【js資料結構】可逐次添加葉子的二叉樹(非最優二叉樹)

來源:互聯網
上載者:User

標籤:遍曆   列印   函數   二叉樹的遍曆   添加   amp   技術   最優   bsp   

最近小菜鳥西瓜瑩看到了一道面試題:

  給定二叉樹,按層列印。例如1的子節點是2、3, 2的子節點是3、4, 5的子節點是6,7.

  需要建立二叉樹:

 

  但是西瓜瑩找到的相關代碼都是用js構建最優二叉樹,假如,依次向二叉樹中添加4,2,6,1,3,5,7最優二叉樹

  比根節點小的節點總是放在根節點的左節點上,比根節點大的節點總是放在根節點的右節點上。

  假如想依次向二叉樹添加1,2,3,4,5,6,7 二叉樹又是如何呢?

  後一個資料總是比前一個資料大,那麼就會一直在根節點的右節點上。

  構建最優二叉樹的核心就是添加節點的函數,記錄當前節點,如果新資料比當前節點小,則讓新資料成為左節點。否則成為右節點。

  代碼如下:

function addNode(ele){    var n = new Node(ele, null, null);    var Que = [];    if(this.root == null)    {        this.root = n;    }    else {            var curr = this.root;            var parent;             while(true)             {                 parent = curr;                 if(ele<curr.element)                 {                     curr = curr.left;                     if( curr == null)                     {                     parent.left = n;                     break;                     }                 }                 else{                     curr = curr.right;                     if(curr == null)                     {                         parent.right = n;                         break;                     }                 }             }         }    }} 

  可是目的並不是建立最優二叉樹,而是建立1,2,3,4,5,6,7這樣的二叉樹,那麼如何?呢?

  思路:用一個隊列來儲存節點,將根節點放入隊列中,如果根節點既有左節點又有右節點,將左右節點放入隊列,如果沒有左節點,將新資料放在左節點上,如果沒有右節點,就將新資料放在右節點上。

  代碼:

function addNode(ele){    var n = new Node(ele, null, null);    var Que = [];    if(this.root == null)    {        this.root = n;    }    else {        var curr = this.root;        Que.push(curr);    }    while(Que.length>0)    {        curr = Que.shift();        if(curr.left != null && curr.right != null)        {            Que.push(curr.left);            Que.push(curr.right);        }        else if(curr.left == null )        {            curr.left = n;            break;        }        else if(curr.right == null)        {            curr.right = n;            break;        }    }} 

  

  

 二叉樹的遍曆

 

  層次遍曆也採用隊列思想:

function showLevel(node){    var queue = [];    var curr;    if(this.root != null)    {        queue.push(this.root)    }    while(queue.length>0)    {        curr = queue.shift();        console.info(curr.element)        if(curr.left != null && curr.right != null)        {            queue.push(curr.left);            queue.push(curr.right);        }        else if(curr.left == null )        {            continue;        }        else if(curr.right == null)        {            queue.push(curr.left);            continue;        }    }}

 執行結果:

 

  

  先序遍曆:

function showTree(node){    if(!(node == null))    {        node.show();        showTree(node.left);        showTree(node.right);    }}

  運行結果:

 

  

完整代碼:

function Node(ele, left, right){    this.element = ele;    this.left = left;    this.right  = right;    this.show = show;}function show(){    console.log(this.element);}function BinaryTree(){    this.root = null;    this.addNode = addNode;    this.showTree = showTree;    this.showLevel = showLevel;}function addNode(ele){    var n = new Node(ele, null, null);    var Que = [];    if(this.root == null)    {        this.root = n;    }    else {        var curr = this.root;        Que.push(curr);    }    while(Que.length>0)    {        curr = Que.shift();        if(curr.left != null && curr.right != null)        {            Que.push(curr.left);            Que.push(curr.right);        }        else if(curr.left == null )        {            curr.left = n;            break;        }        else if(curr.right == null)        {            curr.right = n;            break;        }    }}function showTree(node){    if(!(node == null))    {        node.show();        showTree(node.left);        showTree(node.right);    }}function showLevel(node){    var queue = [];    var curr;    if(this.root != null)    {        queue.push(this.root)    }    while(queue.length>0)    {        curr = queue.shift();        console.info(curr.element)        if(curr.left != null && curr.right != null)        {            queue.push(curr.left);            queue.push(curr.right);        }        else if(curr.left == null )        {            continue;        }        else if(curr.right == null)        {            queue.push(curr.left);            continue;        }    }}var Tree = new BinaryTree();Tree.addNode(1)Tree.addNode(2)Tree.addNode(3)Tree.addNode(4)Tree.addNode(5)Tree.addNode(6)Tree.addNode(7)showTree(Tree.root)Tree.showLevel(Tree.root)

 

  這樣,我們就構建了一個二叉樹:

 

【js資料結構】可逐次添加葉子的二叉樹(非最優二叉樹)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.