js:資料結構筆記9--二叉樹

來源:互聯網
上載者:User

標籤:blog   io   ar   java   strong   sp   資料   div   on   

樹:以分層的方式儲存資料;節點:根節點,子節點,父節點,葉子節點(沒有任何子節點的節點);層:根節點開始0層;

二叉樹:每個節點子節點不超過兩個;尋找快(比鏈表),添加,刪除快(比數組);

BST:二叉樹尋找:

  • 設定根節點為當前節點;
  • 如果要插入的節點小於當前節點,則設定其左節點為新的當前節點;大於的話選右節點;
  • 如果如果選擇的節點為null,則將要插入的節點放在這個位置,退出;否則繼續向下尋找;

實現的基本代碼:

 function Node (data,left,right) {   this.data = data;   this.show = sh } function show() {   console.log(this.data); } function BST() {   this.root = root;   this.insert = insert; } function insert(data) {   var n = new Node(data,null,null);   if(this.root === null) {     this.root = n;  } else {     var currNode =  this.root,parent;     while(true) {       parent = currNode;       if(data < currNode.data) {         currNode = currNode.left;         if(currNode === null) {           parent.left = n;           break;         }       } else {         currNode = currNode.right;         if(currNode === null) {           parent.right = n;           break;         }       }     }   } }

  

 

js:資料結構筆記9--二叉樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.