7 Trees 2 By Juthawut Chantharamalee Data Structure
บทท 7 ทร (Trees) )สวนท 2( By Juthawut Chantharamalee วชาโครงสรางขอมล (Data Structure) รหส 4122202 1
Algorithm findsmallest. BST (root) This algorithm finds the smallest node in a BST. Pre root is a pointer to a nonempty BST or subtree Return address of smallest node 1 if (left subtree empty) 1 return (root) 2 end if 3 return findsmallest. BST (left subtree) end findsmallest. BST อกอรทมท 7. 9 การคนหาโหนดทมคาตำสดในไบนารเสรชทร วชาโครงสรางขอมล (Data Structure) รหส 4122202 18
Algorithm find. Largest. BST (root) This algorithm finds the largest node in a BST. Pre root is a pointer to a nonempty BST or subtree Return address of largest node retruned 1 if (right subtree empty) 1 return (root) 2 end if 3 return find. Largest. BST (right subtree) end find. Largest. BST อกอรทมท 7. 10 การคนหาโหนดทมคาสงสดในไบนารเสรชทร วชาโครงสรางขอมล (Data Structure) รหส 4122202 20
Sequence array 12 18 20 23 35 44 52 23 44 18 12 20 35 52 Search point in binary search รปท 7. 23 ไปนารทรเสรชทรกบวธการคนหาแบบไบนาร วชาโครงสรางขอมล (Data Structure) รหส 4122202 22
Algorithm search. BST (toot, target. Key) Search a binary search tree for a given value. Pre root is the root to a binary tree or subtree target. Key is the key value requested Return the node address if the value is found null if the node is not in the tree 1 if (empty tree) Not found 1 return null 2 end if 3 if (target. Key < root) 1 return search. BST (left subtree, target. Key) 4 else if (target. Key > root) 1 return search. BST (right subtree, target. Key) 5 else Found target key 1 return root 6 end if end search. BST อกอรทมท 7. 11 การคนหาโหนดทมคาสงสดในไบนารเสรชทร วชาโครงสรางขอมล (Data Structure) รหส 4122202 23
23 23 18 18 44 20 12 35 52 44 20 12 (a) Before inserting 23 18 19 18 44 20 52 (b) After inserting 19 19 23 12 35 35 52 20 12 19 38 (c) After inserting 38 44 รปท วชาโครงสรางขอมล 7. 24 การแทรกโหนดใน BTS 35 23 52 38 (d) After inserting 38 (Data Structure) รหส 4122202 25
Algorithm add. BST (root, new. Node) Insert node containing new data into BST using recursion. Pre root is address of current node in a BST New. Node' is address of node containing data Post new. Node inserted into the tree Return address of potential new tree root 1 if (empty tree) 1 set root to new. Node 2 return new. Node 2 end if Locate null subtree for insertion 3 if (new. Node < root) 1 return add. BST (left subtree, new. Node) 4 else 1 return add. BST (right subtree, new. Node) 5 end if end add. BST อกอรทมท 7. 12 การแทรกโนดทในไบนารเสรชทร วชาโครงสรางขอมล (Data Structure) รหส 4122202 26
new. Node < root go left 23 19 12 new. Node 9 new. Node > root go left 20 20 23 12 20 9 23 23 12 9 12 20 Subtree empty Insert here รปท วชาโครงสรางขอมล 9 20 19 7. 24 การแทรกโหนดใน BTS (d) After inserting 38 (Data Structure) รหส 4122202 27
Algorithm delete. BST (root, dlt. Key) This algorithm deletes a node from a BST. Pre root is reference to node to be deleted dlt. Key is key of node to be deleted Post node deleted if dltkey not found, root unchanged Return true if node deleted, false if not found 1 if (empty tree) 1 return false 2 end if 3 if (dlt. Key < root) 1 return delete. BST (left subtree, dlt. Key) 4 else if (dtl. Key > root) 1 return delete. BST (right subtree, dlt. Key) 5 else Delete node found—test for leaf node 1 if (no left subtree) 1 make right subtree the root 2 return true 2 else if (no right subtree) 1 make left subtree the root 2 return true 3 else 1 save root in delete. Kode 2 set largest to largest. BST (left subtree) 3 move data in largest to delete. Node 4 return delete. BST (left subtree of delete. Node, key of largest) 4 end if 6 end if end delete. BST อกอรทมท 7. 13 การลบโหนดออจากไบนารเสรชทร วชาโครงสรางขอมล (Data Structure) รหส 4122202 30
Node to be deleted 17 23 9 17 9 23 23 del. Key 5 11 21 17 Largest key on Left subtree 20 (a) Find del. Key รปท 20 22 22 (b) Find largest 7. 26 แสดงขนตอนการลบโหนดออกจาก กรณโหนดทลบมสองซบทร วชาโครงสรางขอมล (Data Structure) รหส BTS 4122202 31
Move largest data here 17 9 5 17 9 22 11 21 20 17 11 21 17 Largest node deleted 20 22 (c) Move largest data รปท 5 22 (d) Delete largest Node 7. 26 แสดงขนตอนการลบโหนดออกจาก กรณโหนดทลบมสองซบทร วชาโครงสรางขอมล (Data Structure) รหส BTS 4122202 32
1 +หมายถง ซายสง (Left High : LH) 0 หมายถง สงเทากน (Even High : EH) - 1 หมายถง ขวาสง (Right High : RH) HL = 1 12 3 EH 18 LH 23 LH 20 EH HR = 2 44 RH 52 EH 14 EH EH (a) Tree 23 appears balanced: HL – HR = 1 (b) Tree 18 appears balanced: HL – HR = 1 HL = 0 44 HR = 1 18 HR = 1 HL = 2 RH LH 52 12 20 EH 3 EH EH 8 14 (c) Tree 44 appears balanced: HL – HR = 1 14 EH EH รปท 7. 28 เอวแอลทร (AVL Tree) 8 35 วชาโครงสรางขอมล (Data Structure) รหส 4122202 8
Rotation right 20 LH 18 Unbalanced (Left of Left) LH 12 EH 20 EH 12 EH (a) After inserting 12 รปท 7. 30 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ ในรปแบบอยางงาย วชาโครงสรางขอมล (Data Structure) รหส (b) After rotation Left of Left 4122202 43
Rotation right Unbalanced (Left of Left) 18 LH 12 EH 20 8 EH LH 18 EH 8 14 4 14 20 LH EH EH 4 EH (a) After inserting 4 รปท (b) After rotation 7. 31 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ ทซบซอนยงขน (Data Structure) รหส วชาโครงสรางขอมล Left of Left 4122202 44
Rotation left 18 13 Unbalanced (Right of Right) RH 12 18 EH EH RH 20 EH (a) After inserting 20 รปท 7. 30 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ Right ในรปแบบอยางงาย วชาโครงสรางขอมล (Data Structure) รหส (b) After rotation Right of 4122202 46
Rotation left Unbalanced (Left of Left) 14 20 RH 12 EH EH 20 14 LH EH 23 RH 18 23 12 18 44 EH RH EH EH EH 44 EH (a) After inserting 44 รปท (b) After rotation 7. 31 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ Right ทซบซอนยงขน (Data Structure) รหส วชาโครงสรางขอมล Right of 4122202 47
23 Second rotate 18 right LH First rotate 12 left 18 RH 12 20 RH EH 4 EH รปท LH 18 44 LH 23 44 RH LH 52 EH 14 20 LH EH 14 12 16 RH LH EH 16 4 EH EH (a) Original tree (b) After left rotation 7. 35 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ Right of Left ทซบซอนยงขน (Data Structure) รหส 4122202 วชาโครงสรางขอมล 52 EH 50
Second rotation right First rotation right 18 18 RH RH 12 44 12 23 EH LH EH RH 23 LH 20 52 LH EH 20 RH 52 23 EH EH LH (a) Original tree รปท 44 18 44 EH RH (b) After right rotation 12 20 52 EH EH EH (c) After left rotation 7. 37 ตวอยางการหมนโหนดทไมดลไปทางขวากรณ ทซบซอนยงขน (Data Structure) รหส วชาโครงสรางขอมล Left of Right 4122202 54
Algorithm AVLInsert (root, new. Data) Using recursion, insert a node into an AVZ, tree. Pre root is pointer to first node in AVL tree/subtree new. Dmt® is pointer to new node to be inserted Post new node lias been inserted Return root returned recursively up the tree 1 if (subtree empty) Insert at root 1 insert new. Data at root 2 return root 2 end if 3 if (new. Data < root> 1 AVLInsert (left. Subtree, new. Data} 2 if (left. Subtree taller) 1 left. Balance (root) 3 end if 4 else New data >= root data 1 AVLInsert (right. Subtree, new. Ptr, 2 if(right. Subtree taller) 1 right. Balance (root) 3 end if 5 end if 6 return root end AVLInsert อกอรทมท วชาโครงสรางขอมล 7. 14 การแทรกโหนดในเอวเอลทร (Data Structure) รหส 4122202 55
Algorithm left. Balance (root) This algorithm is entered when the root is left high (the leftsubtree is higher than the right. Subtree). Pre root is a pointer to the root of the [sub]tree Post root has been updated (if necessary) 1 if (leftsubtree high) 1 rotate. Right (root) 2 else 1 rotate. Left (leftsubtree) 2 rotate. Right (root) 3 end if end left. Balance อกอรทมท 7. 15 การปรบความสมดลดานซายในเอวแอลทร Left Balance) วชาโครงสรางขอมล (Data Structure) รหส 4122202 (AVL Tree 56
Algorithm rotate. Right (root) This algorithm exchanges pointers to rotate the tree right Pre root points to tree to be rotated Post node rotated and root updated 1 exchange left. Subtree with right. Subtree of left. Subtree 2 make left. Subtree new root end rotate. Right Algorithm rotate. Left (root) This algorithm exchanges pointers to rotate the tree left. Pre root points to tree to be rotated Post node rotated and root updated 1 exchange right. Subtree with left. Subtree of right. Subtree 2 make right. Subtree new root end rotate. Right อกอรทมท 7. 16 การหมนขวาและซายในเอวแอลทร Right and Left) วชาโครงสรางขอมล (Rotate AVL Tree (Data Structure) รหส 4122202 57
Algorithm reheap. Up (heap^ new. Node) Reestablishes hemp by moving data in child up to its correct location in the heap array, Pre heap is array containing an invalid heap new. Node Is index location to new data in heap Post heap has been reordered 1 if (new. Wode not the root) 1 set parent to parent of new. Kode 2 if (new. Node key > parent key) 1 exchange new. Node and parent 2 reheapup (heap, parent) 3 end if 2 end if end reheap. Up อกอรทมท วชาโครงสรางขอมล 7. 17 Reheap Up (Data Structure) รหส 4122202 62
42 21 16 13 42 13 15 10 20 20 25 30 13 (b) Last element (25) move up 32 21 15 10 20 30 12 (c) Moved up again: tree is a heap 7. 42 ตวอยางการ วชาโครงสรางขอมล 25 16 12 รปท 30 (a) Original tree: not a heap 32 25 12 42 21 16 32 Reheap Up (Data Structure) รหส 4122202 63
Algorithm reheap. Down (heap, root, last) Reestablishes heap by moving data in root down to its correct location in the heap. Pre heap is array of data root is root of heap or subheap last is an index to the last element in heap Post heap has been restored Determine which child has larger key 1 if (there is a left subtree) 1 set left. Key to left subtree key 2 if (there is a right subtree) 1 set rightkey to right subtree key 3 else 1 set right. Key to null key 4 end if 5 if (left. Key > right. Key) 1 set large. Subtree to left subtree 6 else 1 set large. Subtree to right subtree 7 end if Test if root > larger subtree 8 if (root key < large. Subtree key) 1 exchange root and large. Subtree 2 reheap. Down (heap, large. Subtree, last) 9 end if 2 end if end reheap. Down อกอรทมท วชาโครงสรางขอมล 7. 18 Reheap Down (Data Structure) รหส 4122202 65
10 21 16 13 20 30 32 (a) Original tree: not a heap 21 13 12 15 32 16 32 21 10 12 30 30 20 16 13 15 (b) Root move down (right) รปท 12 20 10 15 (c) Moved up again: tree is a heap 7. 43 ตวอยางการ วชาโครงสรางขอมล 30 Reheap Down (Data Structure) รหส 4122202 66
Algorithm insert. Heap (heap, last, data) Inserts data into heap. Pre heap is a valid heap structure list is reference parameter to last node in heap data contains data to be inserted Post data have been inserted into heap Return true if successful; false if array full 1 if (heap full) 2 end if 3 increment last 4 move data to last node 5 reheap. Up (heap, last) 6 return true end insert. Heap อกอรทมท วชาโครงสรางขอมล 7. 19 Insert Heap (Data Structure) รหส 4122202 68
Algorithm delete. Heap <heap, last, data. Out) Deletes root of heap and passes data bask to caller. Pre heap is a valid heap structure list is reference parameter to last node in heap data. Out is reference parameter for output area Post root deleted and heap rebuilt Return true if successful; false if array empty 1 if (heap empty) 1 return false 2 end if 3 set data. Out to root data. 4 move last. data to root 5 decrement last 6 reheap. Down (heap, 0, last) 7 return true end delete. Heap อกอรทมท วชาโครงสรางขอมล 7. 20 Delete Heap Node (Data Structure) รหส 4122202 73
The End Lesson 7 (Session 2) วชาโครงสรางขอมล (Data Structure) รหส 4122201 74
- Slides: 74