Tree Lecturers Text Boontee Kruatrachue Kritawan Siriboon Room
Tree Lecturers : Text : รศ. ดร. บญธร Boontee Kruatrachue Kritawan Siriboon Room no. 913 Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 21
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. Top Down Tree 9. B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 22
Height of node = longest path length from node to leaf ��� leaf Height of A = ? path �������� node ���� Tree Height = 3 Height of A = 3 r A ABD length = 2 B Tree Height = Root Heigth ACE length = 2 D ACFG length = 3 C E F Leaf Height = 0 Height of C = 2 G Height is from leaf up to the node Empty Tree Height = -1 รศ. ดร. บญธร เครอตราช Whose height = 1 ? รศ. กฤตวน ศรบรณ KMITL B, F 01076249 Data Structures & Algorithms : Tree 23
Why Balanced Tree ? Tree Efficiency (searching, inserting, deleting, . . . ) → O(Height) 1 4 Binary Search Tree 1 sometimes turn degenerated 5 3 Linear Linked list or near Linear Search O(n) 7 → 7 2 2 6 3 6 5 4 Binary Search Tree → (Perfect) Balanced Tree O(log 2 n) Every leaf is at the same level (very rear case) Balanced Binary Search Tree → Hight Balanced Tree O(log 2 n) → AVL Maximum height <= 1. 44 log 2 n Every node’s subtrees’ heights differ <= 1 - / Height Balanced รศ. ดร. บญธร - - - Height Balanced เครอตราช - - / / 5 - 1 - - Height Balanced รศ. กฤตวน ศรบรณ / 2 3 4 / 8 7 / - - Height Balanced KMITL // - - - Non-Height Balanced 01076249 Data Structures & Algorithms : Tree 24
Balance Factor Height Balanced Tree : (Nearly Balanced) Every node’s subtrees’ heights differ <= 1 Balance factor of every node is 0 or 1 or -1 Balance factor of a node : different of left & right subtrees’ hights = Height(Left Subtree) – Height(Right Subtree) - - 0 0 / - 0 0 - - - 0 0 0 - - 0 Height Balanced Every node’s balance factor = 0 0 -1 -1 -0 Height Balanced 1 0 -(-1) -1 1 -2 Non - Height Balanced เครอตราช 0 - - 0 0 1 0 -(-1) Height Balanced // -1 -1 -0 0 รศ. ดร. บญธร / / - - 1 0 -(-1) 0 รศ. กฤตวน ศรบรณ KMITL 2 1 -(-1) 0 01076249 Data Structures & Algorithms : Tree 25
AVL (Height Balanced) Tree AVLTree Self- rebalancing binary search tree to keep height balance. • Maximum height <= 1. 44 log 2 n : near the ideal complete binary tree. • Search, insertion, deletion ( both average & worst cases) O(log 2 n) • Insertions & deletions may require 1 or > 3 rotations. • Worst case not much slower than random binary search tree. Adelson-Velskii Landis (1962 Soviet inventors) 5 / 2 1 - 3 8 7 4 - / /// \ // - Height Balanced / \ - Bad Binary Tree // / - - - Balance at root is not enough. Smallest AVL tree of height 9 - Non-Height Balanced รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 26
Searching in AVL = Searching in BST AVL is a binary search tree. Review Searching a BST (Binary Search Tree). • Compare search key against key in node start from root. • Follow associated link (recursively). − Left link if search key < key in node − Right link if search key ≥ key in node Ex. Search for 3 3<5 5 3≥ 2 2 1 found 3 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 4 3 7 8 9 3<4 KMITL 01076249 Data Structures & Algorithms : Tree 27
Review Inserting in BST • Search to bottom for key. 5 2 1 NULL Ex. Insert 3 - 8 - 7 - - 1 9 - / - Not found 3 เครอตราช 5 2 8 / รศ. ดร. บญธร 9 Ex. ert 6 5 2 1 / - - 8 - 7 - 9 - Not found 6 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 28
Review Inserting in BST • Search to bottom for key. • Insert node as a new leaf node. 5 2 1 NULL Ex. Insert 3 1 - - 8 - 7 - - - 2 8 - 3 - 7 1 9 - - - 8 - / 7 - 9 - 6 Not found 6 - Insert 3 as a new leaf เครอตราช 5 - Not found 3 รศ. ดร. บญธร 9 Ex. Insert 6 5 2 / / - รศ. กฤตวน ศรบรณ Insert 6 as a new leaf KMITL 01076249 Data Structures & Algorithms : Tree 29
Inserting in AVL without Rebalancing h = 2 5 h = 1 Inserting at short side or equal side 2 h = 0 → AVL ‘s still height balanced 8 h =-1 / 1 § Insert BST new leaf node. h = 1 - 3 - - h =-1 Long Side Short Side § Inserting node will change the height of some nodes (increase by 1) on the path from the newly inserted node to the root only. 7 - h =-1 Equal Side Ex. Insert 6 at equal side. 5 5 - 2 1 - 2 8 - 3 - 7 1 9 เครอตราช รศ. กฤตวน ศรบรณ 8 / / 7 / 9 - 6 insert 3 at short side still height balance รศ. ดร. บญธร - - insert 6 at equal side still height balance KMITL - 6 § Change balance factors along the path to the root. Ex. Insert 3 at short side. 9 01076249 Data Structures & Algorithms : Tree 210
Insertion Re-balancing in AVL h=4 h=3 x h=2 40 - h=1 10 30 - - 5 15 80 60 h=1 / h=1 45 - 25 35 43 46 / 70 Long Side 50 h=0 20 65 - - 55 - 1 st unbalanced 90 / 85 75 - 87 Short Side - 95 - Hight diff = 2 x // / h=1 40 20 52 57 60 h=3 h=2 10 30 - 15 - - - 55 x = 1 st unbalanced node z height of the subtree § → Only some part of the shaded subtree’s balance facters need changing. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 20 10 → ie. Nor height of root. KMITL - 87 95 - - h=4 80 / 40 h=2 y - - - 60 h=1 70 50 30 15 x h=1 - h=1 / h=0 § 75 Short Side h=3 2. Need rebalancing the (blue) shaded subtree. § / 85 Re-balance Causes some node(s) unbalanced (height diff = 2) only up the inserted path. Rebalancing does not change : - 65 Inserting AVL in a long side. § 90 70 50 45 // h=1 Height Unbalanced → no longer AVL ! Long Side 1. 80 h=4 h=1 / h=0 Insert 15 h=5 45 - 90 85 55 - 65 - 87 - - - / 75 - Height balanced again→ AVL 01076249 Data Structures & Algorithms : Tree 211 95 -
4 Insertions Re-balancing in AVL 80 x 40 - 20 - 10 5 - 15 25 - 35 1. Left_Left 43 - / 46 - 52 75 - Short Side 55 - - / 85 87 3 90 70 65 - 45 30 / 60 50 Long Side 10 95 - 1 - 5 20 7 - - 30 15 - - 25 40 - 32 2. Right_Left - - 37 - 60 Long Side 45 41 - 46 4. Left_Right Inserting AVL in a long side. 55 51 - - 59 65 62 - 2. right subtree of left son of x x x left son of x y z 3. right subtree of right son of x z 4. left subtree of right son of x x x y right son of x z เครอตราช z รศ. กฤตวน ศรบรณ KMITL 70 3. Right_Right x = 1 st unbalanced node , inserted node is at 1. left subtree of left son of x รศ. ดร. บญธร 50 Short Side 35 57 x 01076249 Data Structures & Algorithms : Tree 212
Rebalancing Left Subtree of Left Son of x (1 st Unbalanced Node) h=4 x h=2 40 - h=1 80 h=3 60 h=1 / 70 h=1 20 Long Side 50 h=0 - h=0 10 30 - 5 15 - 45 - 25 35 43 46 / 65 - - 55 - 90 85 75 - 1 st Short Side / 95 87 - - son of y : z 40 Re-balance Left subtree of left son of x T 1 - x down to be y’s (right) son. § y up to replace x’ s place : be son of x’s father. 50 65 - 90 85 75 - T 3 - / 87 - h=4 80 h=3 z h=1 h=2 y - 20 60 h=1 70 50 T 1 15 x / h=0 h=1 30 T 0 - / 40 45 - 90 85 87 - - - / T 2 55 65 T 3 75 - - - Height balanced again→ AVL T 0 ≤ z ≤ T 1 ≤ y ≤ T 2 ≤ x ≤ T 3 3. Fixing some part of the shaded subtree’s balance facters. 4. Height of the blue shaded subtree doesn’t change -> also the whole AVL‘s. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL - - 2. Re-arrange T 0, T 1, T 2, T 3 to preserve the properties of BST. § 95 Height Unbalanced → no longer AVL ! § 70 - 10 Right rotate at x // // Short Side 45 T 2 55 30 15 h=1 - h=1 / h=0 10 60 h=1 / 20 80 h=4 h=3 T 0 Long Side x = 1 st unbalanced node. Along the path, let y = son of x, z = son of y. 1. hight diff son of x : y = 2 h=2 Insert 15 52 57 unbalanced x h=5 01076249 Data Structures & Algorithms : Tree 213 95 -
Proof : Rebalancing Area’ Height’s unchange (Left Subtree of Left son of x) Insert at a long side T 0 x = 1 st unbalanced node, y = son of x, z = son of y. Before insertion : • height(T 1) = ? When height(T 0) = h-1 or h ∵ height. Diff (T 0, T 1 ) ≤ 1 & ≠ h+1 ∵ T 1 : short side, T 0 : long sideh+3 x =h ∵ After inserting , z : balanced ∴ height. Diff ≤ 1 h+2 ? y • height(T 2) = h+1 ∵ After inserting , y : balanced T 3 ? h+1 • height(T 3) = h+1 ∵ After inserting , x : unbalanced z T 2 h ? 1. Rebalancing does not change heights of the subtree → ie. Nor of root. 2. So only fix balance facters of inserted path to y and of x. 1 st unbalanced h+2 / T 0 T 1 Long Side T 2 h+3 // h+1/h+2 T 3 h+1 - h/h+1 h - h-1/h x x y z h+4 1 st Diff > 1 h+3 balanced H. Diff ≤ 1 y h+2 / h+1 z h+1 h T 2 T 0 / T 3 T 1 Insert at T 0 เครอตราช รศ. กฤตวน ศรบรณ Right Rotate at x y - h+2 z h+1 / T 0 h+3 h T 1 h+2 x h+1 - h+1 T 2 T 3 - Left Subtree of Left son of x (1) รศ. ดร. บญธร T 0 T 1 Long Side KMITL 01076249 Data Structures & Algorithms : Tree 214
Proof : Rebalancing Area’ Height’s unchange (Left Subtree of Left son of x)2 Insert at a long side T 1 x = 1 st unbalanced node, y = son of x, z = son of y. Before insertion : • height(T 0) = ? When height(T 1) = h-1 or h ∵ height. Diff (T 0, T 1 ) ≤ 1 & ≠ h+1 ∵ T 0 : short side, T 1 : long side h+3 =h ∵ After inserting , z : balanced ∴ height. Diff ≤ 1 x • height(T 2) = h+1 ∵ After inserting , y : balanced h+2 ? y • height(T 3) = h+1 ∵ After inserting , x : unbalanced T 3 ? h+1 1. Rebalancing does not change heights of the subtree → ie. Nor of root. 2. So only fix balance facters of inserted path to y and of x. z ? T 0 x h+2 - z h-1/h. T 0 h x h+3 // h+1/h+2 y h+1 h+4 1 st Diff > 1 h/h+1 T 2 T 1 Long Side T 3 balanced H. Diff ≤ 1 Insert at T 1 y / h+1 h+2 h T 0 z h+1 T 3 T 2 T 1 เครอตราช รศ. กฤตวน ศรบรณ h+3 Right Rotate at x y - h+2 h T 0 z h+1 T 1 h+2 x h+1 - h+1 T 2 T 3 - Left Subtree of Left son of x (2) รศ. ดร. บญธร T 2 T 1 Long Side 1 st unbalanced h+3 h KMITL 01076249 Data Structures & Algorithms : Tree 215
4 Kinds of Rebalancing in AVL x = 1 st unbalanced node, y = son of x, z = son of y 1. Left subtree of left son of x x T 0 y x T 1 T 2 T 3 y T 3 T 0 or T 1 z x 1 z T 3 T 2 T 0 2 y y z 2. Right subtree of left son of x z T 0 T 1 x T 1 T 3 or T 2 or 4. Left subtree of right son of x 3. Right subtree of right son of x 2 y T 0 x y x z T 1 T 2 T 0 T 3 T 1 T 2 or T 1 T 2 y x T 3 z T 0 T 2 T 1 or or or รศ. ดร. บญธร y T 0 T 3 z 1 เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 216 T 3
Proof : Rebalancing Area’ Height’s unchange (Right Subtree of Left son of x) Insert at a long side T 1 x = 1 st unbalanced node, y = son of x, z = son of y. Before insertion : • height(T 2) = ? When height(T 1) = h-1 or h ∵ height. Diff (T 2, T 1) ≤ 1 & ≠ h+1 ∵ T 2 : short side, T 1 : long side =h ∵ After inserting , z : balanced ∴ height. Diff ≤ 1 • height(T 0) = h+1 ∵ After inserting , y : balanced • height(T 3) = h+1 ∵ After inserting , x : unbalanced 1. Rebalancing does not change heights of the subtree → ie. Nor of root. 2. So only fix balance facters of inserted path to z and of y. 1 st Diff > 1 1 st unbalanced 2 h+3 x h+2 h/h+1 - h+1 h T 3 z x y h+1 h+2 T 0 เครอตราช z h+1 / - h-1/h T 1 balanced T 1 T 2 Long Side Insert. H. Diff ≤ 1 at T 1 รศ. ดร. บญธร h+4 h+3 // h+1/h+2 y T 0 1 2 T 3 x Left Rotate at y h T 2 z h y h+1 T 0 T 2 T 3 h+3 z - h+2 y h+1 - h+1 T 0 T 1 h+2 h x h+1 T 2 T 3 T 1 - รศ. กฤตวน ศรบรณ // h+1 Right Rotate atx KMITL Right Subtree of Left son of x 01076249 Data Structures & Algorithms : Tree 217
Proof : Rebalancing Area’ Height’s unchange (Left Subtree of Right Son of x ) Insert at a long side T 1 x = 1 st unbalanced node, y = son of x, z = son of y. Before insertion : • height(T 2) = ? When height(T 1) = h-1 or h ∵ height. Diff (T 1, T 2) ≤ 1 & ≠ h+1 ∵ T 2 : short side, T 1 : long side =h ∵ After inserting , z : balanced ∴ height. Diff ≤ 1 • height(T 3) = h+1 ∵ After inserting , y : balanced • height(T 0) = h+1 ∵ After inserting , x : unbalanced 1. Rebalancing does not change heights of the subtree → ie. Nor of root. 2. So only fix balance facters of inserted path to z and of y. 1 st Diff > 1 1 st unbalanced 2 h+3 x h+1 \ h+3 h+1/h+2 h+2 T 0 h 2 h+4 x y h+1 - h/h+1 z - T 1 Long Side T 3 T 0 h+2 / h+1 z T 1 T 2 รศ. ดร. บญธร y h+1 / h-1/h Insert at T 1 เครอตราช - 1 h Right Rotate at y Left Rotate at x x h+1 \ T 0 T 3 - balanced H. Diff ≤ 1 รศ. กฤตวน ศรบรณ - h+2 x h T 2 z h+2 z h+1 T 1 h+3 y h+1 - h+1 h h+1 T 0 T 1 T 2 y h+1 T 3 - Left Subtree of Right son of x KMITL 01076249 Data Structures & Algorithms : Tree 218
Single Right Rotation & Single Left Rotation Algorithms 1. Left subtree of left son of x // Single Right Rotation // Left. Rotate(t) Left. Rotate(node *px) node* py = px->left = py->right = px return py t t px y x py y z T 0 z T 3 T 0 T 2 x T 1 T 2 T 3 or T 1 or 3. Right subtree of right son of x // Single Left Rotation // Right. Rotate(t) Right. Rotate(node *px) node* py = px->right= py->left = px return py t T 0 z T 1 T 2 T 3 z x py y T 0 y px x t T 1 T 2 T 3 or or รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 219
Double Rotations Algorithms // Double Rotation : // Case 2 : Left Rotation then Right Rotation px Right. Left. Rotate(node *px) node* py = px->left py px->left = Right. Rotate(py) py = Left. Rotate(px) return py 2. Right subtree of left son of x 2 z x 1 y y T 3 T 0 z T 0 T 1 x T 1 T 2 T 3 or T 2 or // Double Rotation : // Case 4 : Right Rotation then Left Rotation Left. Right. Rotate (node *px) px node *py = px->right py px->right = Left. Rotate(py) py = Right. Rotate(px) return py 4. Left subtree of right son of x 2 x z 1 y T 0 T 3 z T 1 T 2 y x T 0 T 2 T 1 or or รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 220 T 3
Double Rotations Algorithms // Double Rotation : // Case 2 : Left Rotation then Right Rotation px Right. Left. Rotate(node *px) node* py = px->left py px->left = Right. Rotate(py) py = Left. Rotate(px) return py 2. Right subtree of left son of x 2 z x 1 y y T 3 T 0 z T 0 T 1 x T 1 T 2 T 3 or T 2 or // Double Rotation : // Case 4 : Right Rotation then Left Rotation Left. Right. Rotate (node *px) px node *py = px->right py px->right = Left. Rotate(py) py = Right. Rotate(px) return py 4. Left subtree of right son of x 2 x z 1 y T 0 T 3 z T 1 T 2 y x T 0 T 2 T 1 or or รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 221 T 3
Single Rotation in AVL A<B<C x = 1 st unbalanced node, y = son of x, z = son of y 1. Left subtree of left son of x B x y z z h+1 / T 2 T 0 - h T 0 T 1 or or C y h+2 T 3 B h+3 A A x z h - h+2 x h+1 T 0 T 3 C y h+2 T 2 Tj h+3 h+2 h+1 - h+1 T 1 Tj T 2 T 3 - - 2. Right subtree of right son of x B x A y T 0 T 2 h+1 - h+1 T 3 T 0 or รศ. ดร. บญธร - x T 1 or C h+2 y h+2 z T 1 h+3 z h+1 / T 2 h T 3 B h+3 A h+2 y x h+1 - h+1 T 0 T 1 Tj รศ. กฤตวน ศรบรณ C h+2 - h T 2 z h+1 สรปรปผลล พธ B ตวคากลาง เปน root ใหม เสมอ • Height(Tj) = h ตวเดยว Tj คอตวทคก บ Ti ท insert node ใหมเขาไป • นอกนน height(T) = h+1 หมด T 3 Tj - เครอตราช T 0<T 1<T 2<T 3 KMITL 01076249 Data Structures & Algorithms : Tree 222
Double Rotations in AVL A<B<C x = 1 st unbalanced node, y = son of x, z = son of y T 0<T 1<T 2<T 3 1. Right subtree of left son of x B 2 x 1 y A z - h+2 T 3 T 0 h+3 y h+1 - h+1 z T 1 T 2 T 0 or h+1 T 0 T 3 Tj - y h+1 / C h+2 z h+2 x T 2 h+3 A C h+2 h T 1 B or x h h+1 - h+1 T 2 T 1 Tj - T 3 - 3. Left subtree of right son of x 2 x B 1 A y T 0 T 1 T 2 - x h+1 - h+1 h T 0 T 2 เครอตราช h+3 A y h+1 T 3 - รศ. กฤตวน ศรบรณ x h+2 y h h+1 / T 0 C z h+2 Tj or รศ. ดร. บญธร T 1 or C h+2 z h+2 T 3 z h+3 B สรปรปผลล พธ B ตวคากลาง เปน root ใหม เสมอ • Height(Tj) = h ตวเดยว Tj คอตวทคก บ Ti ท insert node ใหมเขาไป • นอกนน height(T) = h+1 หมด h+1 - h+1 T 2 T 3 Tj KMITL 01076249 Data Structures & Algorithms : Tree 223
What is the Result AVL after insert 70 ? 10 3 1 - 30 5 20 7 - 15 - - 50 - 35 - - รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ - 40 25 KMITL 45 - 60 55 - - 65 - 70 01076249 Data Structures & Algorithms : Tree 224
Result AVL after Insert 70 10 \ 3 1 - 30 5 \ 20 7 - - 15 - 40 25 - - 50 - 35 - 55 45 65 - - 60 70 10 3 1 - 50 5 30 - 7 - 20 15 - 25 - T 1 - รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ - 55 40 35 - 45 - T 2 - KMITL 60 65 - T 3 70 01076249 Data Structures & Algorithms : Tree 225
90 30 70 95 50 60 40 35 รศ. ดร. บญธร 60 45 55 45 เครอตราช 94 20 80 25 75 15 85 97 93 35 65 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 226
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. Top Down Tree 9. B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 227
Which Representations ? 1. Dynamic r 6 3 1 9 5 3. Sequential (Implicit) Array 2. Linked Array left data 0 -1 1 root 4 2 0 3 -1 3 2 4 typedef int T; struct node{ T data; struct node *left, *right; }; typedef struct node; right father -1 -1 2 4 5 3 -1 6 1 4 2 -1 9 D 3 7 A 0 B 1 I 8 A B C D 4 9 10 F G L 11 I F 5 C 2 M 12 G 6 13 14 L M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 228
Sequential Array (Implicit Array) D 3 7 A 0 B 1 I 8 A B C D 4 9 10 L 11 F G I F 5 C 2 M 12 G 6 13 14 L M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 § § งาย ตองกะขนาด array เพมขนอก 1 level # ทเพมมากกวาทมอยเดมอย (คดเตมท full binary tree ( ถากะขนาด array ถก และ tree ใชพนทสวนใหญของ array เชน almost complete binary tree) จะ save space เพราะไมตองมฟลด left, right, father รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 229
Linked Array left r right father 0 -1 1 -1 2 9 -1 4 root 4 2 0 3 -1 3 5 3 -1 2 6 1 4 2 -1 6 3 1 data 9 5 4 § § รศ. ดร. บญธร ตองใช left, right (or father) ฟลด ดง node มาใชจาก pool กลาง available list ตำแหนงของ node ไมเจาะจงเหมอน sequential จำนวน node ถก limited โดยขนาดของ array เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 230
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. Top Down Tree 9. B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 232
n-ary Trees N-ary Tree : Each node has at most n children. A B F E D C G Ternary Tree. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Generic Tree : Each node has unrestricted number of children. roo t T 2 T 1 . T 4 T 3 . . T 10 A C B D H I J P รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ F E K L G M Q KMITL 01076249 Data Structures & Algorithms : Tree 2 N
Implementation of Generic Tree A B C D F E typedef int T; H struct tree. Node{ T data; struct tree. Node *first. Child struct tree. Node *next. Sibling; }; typedef struct tree. Node; I J P G L K M N Q A first. Child B next. Sibling C next. Sibling D next. Sibling E next. Sibling first. Child H F next. Sibling first. Child I J K first. Child P G first. Child L M N next. Sibling Q next. Sibling รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree v Top Down Tree v B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 236
Multiway Search Tree Multiway Tree order n : has at most n subtrees (has 0, 1, . . . or n subtrees) (#max. sons = n) Order 4: มลกไดมากทสด 4 ตว § Ex. tree : root has maximum sons = 4 § How many keys of the father of 4 sons ? § 3 § So, for order 4, #maximum key = 3 For order n : § #max. sons = n § #max. keys = n-1 Multiway Search Tree : for every key K left decendents ≤ K right decendents > K leaf : node ทไมมลกเลย Semi-leaf node : มลกอยางนอย NULLทมจำนวน Fullตวเปน node : node เตมท Order 4 § #max. sons = 4 § #max. keys = 3 leaf = NULL รศ. ดร. บญธร key Full node Semi-leaf leaf Semi-leaf เครอตราช 1 รศ. กฤตวน ศรบรณ Full node leaf KMITL leaf 01076249 Data Structures & Algorithms : Tree 237
Multiway Search Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 238
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree v Top Down Tree v B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 239
Top Down Tree Multiway Search Tree Order n : has at most n subtrees (has 0, 1, . . . or n subtrees) (#max. sons = n) Top Down Tree : Non full node must be leaf. Node ทไมเตมจะเปน leaf ไดเทานน เปน internal node ไมได • Fill up the existing nodes before creating a new node ! เตม node ใหเตมกอนคอยแตก • • node ใหม →have fewer nodes as possible →fewer nodes : shorter height ! Order 3 § #max. sons = 3 § #max. keys = 2 57 Insert 57 Must be with 55 Not in a new node. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 240
Unbalanced Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 241
Balanced Tree : every leaf node is at the same level (Perfect balance , perfectly height-balanced) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 242
Balanced Tree : every leaf node is at the same level (Perfect balance , perfectly height-balanced) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 243
Tree 2 3. 4. AVL Tree Height Balanced Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree v Top Down Tree v B-Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree KMITL 01076249 Data Structures & Algorithms : Tree 244
B-Trees B-tree order n 1. 2. Multi-way search tree order n #max son = n, #max keys = n-1 Balanced tree : every leaf node is at the same level (Perfect balance , perfectly height-balanced) 3. => next page Order 3 § #max. sons = 3 § #max. keys = 2 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 245
B-Trees B-tree order n 1. Multi-way search tree order n #max son = n, #max keys = n-1 2. Balanced tree 3. ทก node มจำนวน non-empty subtree (มลก )อยางนอยทสด เทากบ ครงหนงของ n )มนอยกวานไมได ) (Half Full) ยกเวน root node ไมตอง half full #min sons = n/2 EX. 1. order 2 : #max sons =2 , #min sons = 2/2 = 1 ∴ Binary ทเปน (almost) complete binary tree จะเปน B-tree 2 order 5 : #max sons = 5 , #min sons = 5/2 = 2. 5 = 3 3 order 201 : #max sons = 201 , #min sons = 201/2 = 101 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 246
B-Tree Variation order 5 #max son = 5, #max keys = 4 • Half Full #min sons = 5/2 = 2. 5 = 3 )ยกเวน root node) B-tree -> variations • internal nodes : เกบเฉพาะ key เพอประหยดพนท , order กำหนดจำนวนลก • external nodes : เกบ data ทง record คา L กำหนดจำนวน record/node ของ leaf รศ. ดร. บญธร ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 247 ถา L = 5 : เครอตราช #max รศ. กฤตวน data = 5, #min data = 5/2 = 2. 5 = 3
B-Tree (Variation) Insertion Insert at leaf: insert until full, if full -> split Insertion consider #maximum order 5 : #max son = 5, Internal : #max keys = 4, #min sons = 5/2 = 3 Leaf L = 5: #max data = 5, #min data = 5/2 = 3 รศ. ดร. บญธร เครอตราช consider -> #maximum data/key รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 248
B-Tree (Variation) Insertion order 5 : #max son = 5 : #max keys = 4, not full -> can insert order 5, #max key = 5 -1=4 L=5 #leaf max data = 5 key 57 splits 2 nodes key 57 must be inserted in father node Insert 55 Insert at leaf until full : L = 5: #max data = 5 Full รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ Full -> split half-half by 57 KMITL 01076249 Data Structures & Algorithms : Tree 249
B-Tree (Variation) Insertion 38 Insert at leaf: until full, if full -> split order 5 : #max son = 5 : #max keys = 4, full -> split by 26 Insert 40 Insert at leaf until full : L = 5: #max data = 5 Full -> split by 38 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 250
B-Tree (Variation) Insertion Insert at leaf: until full, if full -> split order 5, #max key = 5 -1=4 L=5 #leaf max data = 5 Insertion consider #maximum order 5 : #max son = 5, Internal : #max keys = 4, #min sons = 5/2 = 3 Leaf L = 5: #max data = 5, #min data = 5/2 = 3 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 251
B-Tree (Variation) Deletion consider #minimum Internal order 5 : #min sons = 5/2 = 3 #min keys = 2 Leaf L = 5: #max data = 5, #min data = 5/2 = 3 Delete at leaf: until low, if low -> borrow Borrowing 1. ยมพนอง รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ (ผานพอ ) เชน del 49 2. ยมพอ ตอง consolidate เชน del 84 พอไมม พอยม -พนองพอ (ผานป ). . . KMITL 01076249 Data Structures & Algorithms : Tree 252
Delete at leaf: B-Tree (Variation) Deletion until low, if low -> borrow Deletion consider #minimum Internal order 5 : #min sons = 5/2 = 3 #min keys = 2 Leaf L = 5: #max data = 5, #min data = 5/2 = 3 Borrowing 1. ยมพนอง รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ (ผานพอ ) เชน del 49 2. ยมพอ ตอง consolidate เชน del 84 พอไมม พอยม -พนองพอ (ผานป ). . . KMITL 01076249 Data Structures & Algorithms : Tree 253
Delete at leaf: B-Tree (Variation) Deletion until low, if low -> borrow Deletion consider #minimum Internal order 5 : #min sons = 5/2 = 3 #min keys = 2 Leaf L = 5: #max data = 5, #min data = 5/2 = 3 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL Borrowing 1. ยมพนอง (ผานพอ ) เชน del 49 2. ยมพอ ตอง consolidate เชน del 84 พอไมม พอยม -พนองพอ (ผานป 01076249 Data Structures & Algorithms) : Tree 254
- Slides: 54