1 / 21

資料結構使用 Java

資料結構使用 Java. 第 10 章 二元搜尋樹 (Binary Search Tree). 課程內容. 二元樹追蹤 前序追蹤 中序追蹤 後序追蹤 二元搜尋樹 建立二元搜尋樹的方法 程式碼撰寫實作 刪除二元搜尋樹. 二元樹的追蹤. 二元樹追蹤 (binary tree traversal) 是樹最重要的基本運算。 追蹤時保存著 3 項重要的資訊: 節點資料 指向左子樹的鏈結 指向右子樹的鏈結。. 二元樹的追蹤. 前序 (Preorder) : 樹根→左子樹→右子樹 (DLR) 中序 ( Inorder ) :

Download Presentation

資料結構使用 Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 資料結構使用Java 第10章 二元搜尋樹 (Binary Search Tree)

  2. 課程內容 • 二元樹追蹤 • 前序追蹤 • 中序追蹤 • 後序追蹤 • 二元搜尋樹 • 建立二元搜尋樹的方法 • 程式碼撰寫實作 • 刪除二元搜尋樹

  3. 二元樹的追蹤 • 二元樹追蹤(binary tree traversal)是樹最重要的基本運算。 • 追蹤時保存著3項重要的資訊: • 節點資料 • 指向左子樹的鏈結 • 指向右子樹的鏈結。

  4. 二元樹的追蹤 • 前序(Preorder): • 樹根→左子樹→右子樹(DLR) • 中序(Inorder) : • 左子樹→樹根→右子樹(LDR) • 後序(Postorder): • 左子樹→右子樹→樹根(LRD)

  5. 前序追蹤(Preorder traversal) • 追蹤順序: • 節點資料 • 拜訪左子樹 • 拜訪右子樹

  6. 【範例一】 • 試寫出下圖之前序追蹤順序。 A D B H C G L F E K J I

  7. 前序追蹤 (遞迴演算法) • 若樹根節點不為空節點,則執行下列步驟:1.走訪樹根 2.走訪左子樹(遞迴)3.走訪右子樹(遞迴) • 下載原程式碼 public void preOrder(){ System.out.print(data+" "); if(left!=null) left.preOrder(); if(right!=null) right.preOrder(); }

  8. 中序追蹤(Inorder traversal) • 追蹤順序: • 拜訪左子樹 • 節點資料 • 拜訪右子樹

  9. 【範例二】 • 試寫出下圖之中序追蹤順序。 A D B H C G L F E K J I

  10. 中序追蹤 (遞迴演算法) • 若樹根節點不為空節點,則執行下列步驟:1.走訪左子樹(遞迴) 2.走訪樹根3.走訪右子樹(遞迴) public void inOrder(){ if(left!=null) left.preOrder(); System.out.print(data+" "); if(right!=null) right.preOrder(); }

  11. 後序追蹤(Postorder traversal) • 追蹤順序: • 拜訪左子樹 • 拜訪右子樹 • 節點資料

  12. 【範例三】 • 試寫出下圖之後序追蹤順序。 A D B H C G L F E K J I

  13. 後序追蹤 (遞迴演算法) • 若樹根節點不為空節點,則執行下列步驟:1.走訪左子樹(遞迴) 2.走訪右子樹(遞迴)3.走訪樹根 public void postOrder(){ if(left!=null) left.preOrder(); if(right!=null) right.preOrder(); System.out.print(data+" "); }

  14. 二元搜尋樹 Binary Search Tree

  15. 二元搜尋樹(Binary Search Tree) • 為一種「有大小順序」的二元樹。 • 任何節點 • 左子樹之值小於節點的值 • 右子樹之值大於節點的值 • 左子樹和右子樹亦是二元搜尋樹。 • 將二元搜尋樹中序追蹤,即可得到排序後由小至大的資料。 50 72 30 95 42

  16. 建立二元搜尋樹 • 建立二元搜尋樹時,必須加入大小排序規則: • 以第一個資料當作樹根 • 之後之資料與樹根比較 • 若小於樹根則成為樹根之左子樹 • 若大於樹根則成為樹根之右子樹 • 如此一直遞迴之進行。

  17. 建立二元搜尋樹 • 輸入資料: 37, 57, 23, 15, 32。

  18. 建立二元搜尋樹-程式碼 • 使用者輸入的第一個數字成為樹根節點(root)。 treeNode root = new treeNode(sc.nextInt()); • 建立insert方法來新增節點。 • 呼叫insert時傳入新節點(newNode)參數。 root.insert(new treeNode(sc.nextInt())); • insert方法判斷目前節點與新節點的data大小 • 新節點data較小:新增到左子樹。 • 新節點data較大:新增到右子樹。 • 若左、右子樹為空,則直接將新節點掛上。

  19. 建立二元搜尋樹-程式碼 if(newNode.data < data){ //新節點data較小 if(left == null) //左子樹為空 left = newNode; else left.insert(newNode);//新增到左子樹 }else{ if(right == null) right = newNode; else right.insert(newNode); }

  20. 50 65 40 60 45 30 刪除二元搜尋樹節點 • 若是樹葉節點:直接刪除 • 若是內部節點: • 在左子樹找數值最大的節點取代之 • 或是在右子樹找數值最小的節點取代之 • 例:刪除右圖50

  21. 60 65 40 45 30 刪除二元搜尋樹節點 • 取右子樹最小的節點 • 或是取左子樹最大的節點 45 65 40 60 30

More Related