Providence University Dept of Computer Science and Information
- Slides: 60
Providence University 資料結構 老師:李崇明 助教:楊斯竣 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 1/24
Providence University 6 -2 二元樹的基礎-圖例 二元樹 右子樹 左子樹 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 10/24
Providence University 6 -3 二元樹的表示法 • 6 -3 -1 二元樹陣列表示法 • 6 -3 -2 二元樹物件陣列表示法 • 6 -3 -3 二元樹鏈結表示法 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 15/24
Providence University 6 -3 -1 二元樹陣列表示法-二元樹類 別 class BTree_Array { // 二元樹類別 private int[] btree; // 二元樹陣列宣告 // 建構子: 建立二元樹 public BTree_Array(int size, int[] array) { … … } // 方法: 顯示二元樹 public void print. BTree() { …… } } 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 19/24
Providence University 6 -3 -1 二元樹陣列表示法-成員方法 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 20/24
Providence University 6 -3 -1 二元樹陣列表示法-顯示二 元樹 print. BTree()方法:顯示二元樹 • print. BTree()方法只是走訪btree[]陣列,將 元素值不是-1的元素都顯示出來。 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 23/24
Providence University 程式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 24/24
Providence University 6 -3 -2二元樹物件陣列表示法-節 點與二元樹類別 class Node { // 樹節點類別 int data; // 節點資料 int left; // 參考左子樹 int right; // 參考右子樹 } class BTree_Node { // 二元樹類別 private Node[] btree; // 二元樹陣列宣告 // 建構子: 建立二元樹 public BTree_Node(int size, int[] data) { …… } // 方法: 顯示二元樹 public void print. BTree() { …… } } 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 27/24
Providence University 6 -3 -2二元樹物件陣列表示法-圖 例 • 例如:一棵二元樹和其物件陣列表示法, 如下圖所示: 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 28/24
Providence University 6 -3 -3 二元樹鏈結表示法-Tree. Node 類別 鏈結串列實作的節點與二元樹類別:Tree. Node和 BTree class Tree. Node { // 樹節點類別 int data; // 節點資料 Tree. Node left; // 參考左子樹 Tree. Node right; // 參考右子樹 // 建構子 public Tree. Node(int data) { …… } } 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 30/24
Providence University 6 -3 -3 二元樹鏈結表示法-BTree類別 public class BTree { // 二元樹類別 public Tree. Node head; // 參考樹的根節點 // 建構子: 使用陣列建立二元樹 public BTree(int[] array) { …… } // 方法: 檢查二元樹是否是空的 boolean is. BTree. Empty() { …… } // 方法: 在二元樹插入節點 public void insert. BTree. Node(int data) { …… } // 方法: 顯示二元樹的節點資料 public void print. BTree() { …… } } 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 31/24
Providence University 6 -3 -3 二元樹鏈結表示法-成員方法 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 32/24
Providence University 6 -3 -3 二元樹鏈結表示法-建立二 元樹(圖例) 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 34/24
Providence University 程式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 36/24
Providence University 6 -4 二元樹的走訪 • 6 -4 -1 中序走訪方式 • 6 -4 -2 前序走訪方式 • 6 -4 -3 後序走訪方式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 37/24
Providence University 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 41/24
Providence University 程式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 43/24
Providence University 6 -4 -1 中序走訪方式-遞迴呼叫 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 44/24
Providence University 6 -4 -2 前序走訪方式-遞迴呼叫 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 47/24
Providence University 程式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 48/24
Providence University 6 -4 -3 後序走訪方式-遞迴呼叫 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 51/24
Providence University 程式 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 52/24
53 Providence University Binary Search Tree 二元搜尋樹 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 53/24
56 Providence University 建立二元搜尋樹 • 輸入資料: 37, 57, 23, 15, 32。 資 訊 程 學 Dept. of Computer Science and Information Engineering 系 56/24
57 Providence University 建立二元搜尋樹-程式碼 • 使用者輸入的第一個數字成為樹根節點 (root)。 tree. Node root = new tree. Node(sc. next. Int()); • 建立insert方法來新增節點。 – 呼叫insert時傳入新節點(new. Node)參數。 root. insert(new tree. Node(sc. next. Int())); 資 • insert方法判斷目前節點與新節點的data大 小 訊 程 學 系 – 新節點data較小:新增到左子樹。 Dept. of Computer Science and Information Engineering 57/24
58 Providence University 建立二元搜尋樹-程式碼 資 if(new. Node. data < data){ //新節點data 較小 if(left == null) //左子樹為空 left = new. Node; else left. insert(new. Node); //新增到左子 樹 }else{ if(right == null) 訊 程 學 Dept. of Computer Science and Information Engineering 系 58/24
60 Providence University 刪除二元搜尋樹節點 60 • 取右子樹最小的節點 • 或是取左子樹最大的節點 45 65 40 資 訊 30 程 65 40 30 45 60 學 Dept. of Computer Science and Information Engineering 系 60/24
- Providence university taiwan ranking
- Providence college library
- What state's capital is hartford
- The village lofts pawtucket
- Kevin manemann providence
- Providence engineering
- Providence engineering
- Providence engineering
- 8 principles of montessori method
- Coincidence or providence
- Providence montessori school
- Abraham journey map
- What are your favorite subjects
- Computer science university of phoenix
- University of bridgeport engineering
- University of bridgeport computer science faculty
- Yonsei computer science
- Florida state university masters in computer science
- York university computer science
- Chapel hill computer science
- Meng computer science ucl
- Seoul national university computer science
- Osaka university computer science
- Computer science columbia university
- 7800 york road
- K state computer science
- Brown university computer science
- Trinity university computer science
- Brandeis university computer science
- University
- Kotebe metropolitan university
- Webnis
- Department of finance and administration
- Florida dept of agriculture and consumer services
- Florida dept of agriculture and consumer services
- Florida dept of agriculture and consumer services
- Dept nmr spectroscopy
- Finance departments
- Worcester building dept
- Dept. name of organization (of affiliation)
- Mn dept of education
- Dept. name of organization
- Ohio dept of developmental disabilities
- Hjdkdkd
- Vaginal dept
- Gome dept
- Gome dept
- Horizontal
- Gome dept
- Hoe dept
- Firefighter interview tips
- Oviposition
- Dept of education
- Dept a
- Central islip fire dept
- Rowan county dss child protective services
- Dept of education
- Tabella chemical shift c13
- Pt dept logistik
- Nys department of homeland security
- La dept of revenue