Tree 1 Tree Definitions 2 Binary Tree Traversals
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees KMITL 01076249 Data Structures & Algorithms : Tree 12
Height of node = longest path length from node to leaf path �������� node ���� leaf Empty Tree Height = -1 Tree Height = Root Heigth =3 Height of A = ? r A ACFG length = 3 Height of A = 3 ABD length = 2 B D ACE length = 2 Leaf Height = 0 Whose height = 1 ? B, F C E F G Tree Height of C = 2 Height: ��� leaf ����� node รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 23
Why Balanced Tree ? Tree Efficiency (searching, inserting, deleting, . . . ) → O(Height) Binary Search Tree sometimes turn degenerated 1 4 1 5 3 2 7 → 6 Linear Linked list or near Linear Search O(n) 3 6 5 4 Binary Search Tree (Perfect) Balanced Tree Every leaf is at the same level (very rear case) Binary Search Tree Hight Balanced Tree → AVL Maximum height <= 1. 44 log 2 n Every node’s subtrees’ heights differ <= 1 O(log 2 n) Balanced Binary Search Tree 5 / 2 1 - 3 4 / 8 7 / O(log 2 n) - - Height Balanced รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 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 รศ. กฤตวน ศรบรณ 2 1 -(-1) 0 KMITL 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 16
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 - - 7 9 - - / 7 - 9 - Not found 3 เครอตราช 5 2 8 - รศ. ดร. บญธร 8 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 - - - 7 9 - - - 2 8 - 3 - 7 9 - - Insert 3 as a new leaf เครอตราช 1 5 - 8 - / 7 - - 9 - 6 Not found 3 รศ. ดร. บญธร 8 Ex. Insert 6 5 2 / / - รศ. กฤตวน ศรบรณ Not found 6 - Insert 6 as a new leaf KMITL 01076249 Data Structures & Algorithms : Tree 29
Inserting in AVL without Rebalancing h = 2 Inserting at short side or equal side 5 h = 1 2 → AVL ‘s still height balanced h = 0 § Insert BST new leaf node. 8 h =-1 / 1 § Inserting node will Long Side change the height of some nodes (increase by 1) on the path from the newly inserted node to the root only. h = 1 - 3 - h =-1 Short Side 7 - h =-1 Equal Side Ex. Insert 6 at equal side. 5 5 - 2 1 - 2 8 - 3 - 7 9 - - insert 3 at short side still height balance รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1 - 6 Change balance factors along the path to the root. Ex. Insert 3 at short side. 9 8 / / 7 - / 9 - 6 - insert 6 at equal side still height balance KMITL 01076249 Data Structures & Algorithms : Tree 210
Insertion Re-balancing in AVL 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 x h=4 h=3 90 / 85 75 - 87 Short Side - 95 - Hight diff = 2 // / h=1 40 20 52 57 60 h=3 h=2 10 30 - 15 - - - 55 x = 1 st unbalanced node z § → Only some part of the shaded subtree’s balance facters need changing. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 20 10 → ie. Nor height of root. - 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. height of the subtree / 85 Re-balance 1. Causes some node(s) unbalanced (height diff = 2) only up the inserted path. § - 65 Inserting AVL in a long side. Rebalancing does not change : 90 70 50 45 // h=1 Height Unbalanced → no longer AVL ! Long Side § 80 h=4 h=1 / h=0 Insert 15 h=5 45 - 90 85 55 - 65 - 87 - - - / 75 - Height balanced again→ AVL KMITL 01076249 Data Structures & Algorithms : Tree 211 95 -
4 Insertions Re-balancing in AVL Inserting AVL in a long side : ให x = node แรกทไม เปนลกของ y balance , ตาม path ท 80 x y z 20 - 10 5 - 15 50 Long Side 25 - 45 30 - / 40 - 43 35 1. Left_Left - 46 / 60 70 z 65 - - 75 - Short Side 55 52 - / 87 3 95 - - 57 1 - 5 30 20 7 - 15 - - z 25 - Short Side 35 32 2. Right_Left - 50 40 - 37 - y 60 Long Side 45 41 - 46 55 51 - - 59 z 65 62 - x left son of x y z 3. right subtree of right son of x x y right son of x z เครอตราช รศ. กฤตวน ศรบรณ z 4. left subtree of right son of x x y right son of x z 70 3. Right_Right 2. right subtree of left son of x x รศ. ดร. บญธร x 4. Left_Right ม 4 กรณ เมอ insert ท 1. left subtree of left son of x x และ z 10 90 85 insert ให y เปนลกของ KMITL 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 60 h=1 / h=1 20 Long Side 50 h=0 - h=0 10 5 - 30 15 - 45 - 25 35 43 46 / 70 65 - - 55 - - 1 st unbalanced x 60 90 85 75 Short Side / 95 87 - - 52 57 h=3 10 T 1 - - 85 75 - T 3 - - / 87 - Re-balance h=4 80 h=3 z h=1 h=2 y 10 - x 60 / h=0 h=1 - h=1 70 50 30 T 1 15 / 40 20 T 0 ������ x) �������� x) 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. รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ - - 2. Re-arrange T 0, T 1, T 2, T 3 to preserve the properties of BST. § 95 Height Unbalanced → no longer AVL ! § x ���� y’s (right) son. 65 90 Short Side 45 T 2 55 T 0 Long Side Right rotate at x 50 - 30 15 // 70 h=1 / 20 h=1 // 40 h=1 / h=0 Insert 15 Left subtree of left son of x § y hight diff son of x : y = 2 h=2 son of y : z x = 1 st unbalanced node. Along the path, let y = son of x, z = son of y. 1. 80 h=4 80 h=3 h=5 KMITL 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. กอน insert: • height(T 1) = ? When height(T 0) = h-1 หรอ h ∵ height. Diff (T 0, T 1 ) ≤ 1 & ≠ h+1 ∵ T 1 : short side, T 0 : longh+3 side =h insert , z : balanced ∴ height. Diff ≤ 1 ∵ หลง • height(T 2) = h+1 ∵ หลง • height(T 3) = h+1 ∵ หลง 1. Rebalancing 2. ดงนน ไมเปลยน จงแค แก subtree → ie. รวมทง ของ ตาม h+2 / y h - h-1/h T 0 T 1 Long Side x T 2 h+3 // h+1/h+2 h+1 - h/h+1 z h+4 1 st Diff > 1 T 3 balanced H. Diff ≤ 1 y h+2 / h+1 z h+1 h T 2 T 0 / T 1 Insert at T 0 - รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ T 3 z root. T 0 inserted path 1 st unbalanced x ของ h Long Side Right Rotate at x z h+1 / T 0 T 2 ? T 1 y h+3 - h+2 h T 1 T 3 ? h+1 height ? y insert , x : unbalanced balance facters h+3 h+2 insert , y : balanced heights x h+2 x h+1 - h+1 T 2 T 3 - Left Subtree of Left son of x (1) 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 1 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ T 2 T 3 T 2 T 1 Long Side 1 st unbalanced h+3 h 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) 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 or x T 1 T 2 T 3 or T 2 or 4. Left subtree of right son of x 3. Right subtree of right son of x 2 y x y T 0 x z T 1 T 2 T 0 T 3 T 1 T 2 or or รศ. ดร. บญธร y T 0 T 3 z 1 T 3 z T 1 T 2 y x T 0 T 2 T 1 or or เครอตราช รศ. กฤตวน ศรบรณ 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 // h+1 T 2 T 3 Right Rotate atx 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 - Right Subtree of Left son of x KMITL 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 h+2 / h+1 z T 1 T 2 รศ. ดร. บญธร y T 0 h+1 / h-1/h Insert at T 1 เครอตราช - 1 h Right Rotate at y T 3 Left Rotate at x x h+1 \ T 0 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 // Single Right Rotation // Left. Rotate(t) Left. Rotate(node *px) node* py = px. left //px->left px. left = py. right = px return py 1. Left subtree of left son of x 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
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 h+2 x z T 2 h Tj T 0 T 3 h+3 A h+2 h+1 - h+1 T 1 y - สรปรปผลลพธ x B ตวคากลาง h+1 - h+1 T 1 Tj C h+2 h+1 T 2 T 3 2. Right subtree of right son of x B 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 Tj เครอตราช รศ. กฤตวน ศรบรณ B A h+2 h+3 y x h+1 - h+1 T 0 C h+2 - T 1 h T 2 เปน rootใหม เสมอ • Height(Tj) = h ตวเดยว Tj คอตวทคกบ Ti ท insert node ใหมเขาไป • นอกนน height(T) = h+ หมด - - x T 0<T 1<T 2<T 3 z h+1 T 3 Tj KMITL 01076249 Data Structures & Algorithms : Tree 221
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 x B 1 A y T 0 T 1 - T 2 x h+1 - h+1 h T 0 T 2 เครอตราช A h+3 y h+1 T 3 รศ. กฤตวน ศรบรณ x h+1 / T 0 C z - h+2 Tj or รศ. ดร. บญธร T 1 B or C h+2 z h+2 T 3 z h+3 h T 1 เปน rootใหม เสมอ • Height(Tj) = h ตวเดยว Tj คอตวทคกบ Ti ท insert node ใหมเขาไป • นอกนน height(T) = h+1 หมด - 2 B ตวคากลาง h+2 y h+1 - h+1 T 2 T 3 Tj KMITL 01076249 Data Structures & Algorithms : Tree 222
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 123
Binary Tree Representations 1. Dynamic r class node: 6 3 1 9 5 def __init__(self, data, left = None, right = None): self. data = data self. left = left self. right = right 2. Sequential (Implicit) Array 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 D 4 G 6 13 14 L M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 8 A 1 B 2 I 9 5 10 A B C D 11 F G L 12 F 6 I C 3 M 13 G 7 14 15 L M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 KMITL 01076249 Data Structures & Algorithms : Tree 124
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 225
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 127
n-ary Trees n-ary Tree ����� node ����� A B E F G Ternary Tree รศ. ดร. บญธร เครอตราช D C รศ. กฤตวน ศรบรณ n=3 KMITL 01076249 Data Structures & Algorithms : Linked List
Generic Tree : ����� node ����������� ���� directory : folder, file root T 2 T 1 . T 4 T 3 . . T 10 A C B D H I J P รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ F E K L G M N Q KMITL 01076249 Data Structures & Algorithms : Linked List
Implementation of Generic Tree A B C D F E H I class node: def __init__(self, data, left = None, right = None): self. data = data self. left = first. Child self. right = next. Sibling J L K P G 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 : Linked List
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 131
Multiway Search Tree Multiway Tree order n : ��������� n ��� (0, 1, . . . or n subtrees) (#max. sons = n) Multiway Search Tree : for every key K For order n : § #max. sons = n § #max. keys = n-1 left decendents ≤ K right decendents > K leaf : node ทไมมลกเลย Full node : ทมจำนวน node keyเตมท รปตวอยาง root node มลก 4 ตว § มลก 4 node ตองม key ? = 3 Full node Order 4 § #max. sons = 4 § #max. keys = 3 Full node leaf = NULL รศ. ดร. บญธร leaf เครอตราช Full node รศ. กฤตวน ศรบรณ Full node leaf KMITL 01076249 Data Structures & Algorithms : Linked List
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 133
Balanced Tree : every leaf node is at the same level (Perfect balance , perfectly height-balanced) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
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 136
B-Trees B-tree order n 1. Multi-way search tree order n : #max son = n, #max keys = n-1 2. Balanced tree : ��� leaf ������ level ���� (Perfect balance , perfectly height-balanced) 3. => next page รศ. ดร. บญธร เครอตราช Order 3 § #max. sons = 3 § #max. keys = 2 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
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 : Linked List
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 ถา L = 5 : #max data = 5, #min data = 5/2 = 2. 5 = 3 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
B-Tree (Variation) Insertion ����� #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 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ Insert ��� leaf: insert �� full, ��� full -> split �� insert ���������� (max) -> Insert ��� leaf ����� L Leaf L = 5: #max data = 5 KMITL 01076249 Data Structures & Algorithms : Linked List
B-Tree (Variation) Insertion order 5 : internal node ��#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 ตองถก insert ใน father node Insert 55 Insert ��� leaf จน full : Full -> split half-half ��� 57 L = 5: #max data = 5 Full รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
B-Tree (Variation) Insertion 38 Insert at leaf: until full, if full -> split order 5 : internal node : #max son = 5 : #max keys = 4, full -> split by 26 Insert 40 Insert ท leaf จน full : L = 5: #max data = 5 Full -> split by 38 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
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 ����� #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 : Linked List
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. ยมพนอง (ผานพอ ) 2. ยมพอ ตอง consolidate พอไมม พอยม -พนองพอ (ผานป ) . . . รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
B-Tree (Variation) Deletion Delete at leaf: 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 เอา 46 ของพมา 2. ยมพอ ตอง consolidate เชน del 84 พนองไมมใหยม ยมพอ โดยรวม consolidate กบพอ ถาพอ ไมม พอยม -พนองพอ (ผานป ) . . . KMITL 01076249 Data Structures & Algorithms : Linked List
B-Tree (Variation) Deletion Delete at leaf: 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. ยมพนอง (ผานพอ ) 2. ยมพอ ตอง consolidate พอไมม พอยม -พนองพอ (ผานป ) . . . รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Linked List
- Slides: 46