Tree Lecturers Text 1 Boontee Kruatrachue Kritawan Siriboon
Tree Lecturers : Text : รศ. ดร. บญธร เครอตราช 1 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 1
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 2 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Tree Definitions A tree can be 1. empty with no nodes called the null tree or empty tree or 2. a tree is a structure consisting of 1. one node called the root node 2. The name of the tree usually called by its root. locating a tree r and one or more subtrees. R anch) r b ( e dg e Node in a tree must be disjoint. It must not be in common in root or subtrees. cts conne ubtree-root s root & A D E subtree A Subtree is also a tree. -root A -subtree D & E tree R H B C F G I subtree B J subtree C K K is not disjoint (be in subtree I & subtree J) → R is not a tree. รศ. ดร. บญธร เครอตราช 3 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Tree Definitions Root has no father. Root = father (parent) of subtree’s root. Subtree’s root = son (child) of his father. r R A D R = father(parent) of A B C E Leaf (leave) = Enternal node. H B C F G I J A B C = sons(childs) of R Father of F ? Sons of A ? Father of R ? Sons of H ? K Leaf (leave) ≠ Internal node. Leaf (leave) = node with no son. รศ. ดร. บญธร เครอตราช 4 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Siblings Leaf Internal Grand Parent Grand Child Leaf node (external, outer, terminal) node with no son r Branch node (internal, inner, inode (for short)) node ≠ leaf Siblings (brothers) node with same father เครอตราช 5 F I Grand Child son of son รศ. ดร. บญธร B E Grand Parent father of father A C D G H J K L รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Path, Path Length, Depth, Height Path (from n to d) sequence of nodes and edges connecting a node n with a descendant d In tree, only 1 path from node to node Depth (level) of node = path length from root to node Height of node = longest path length from node to leaf Depth = Level = 0 Path A, B, F length = 2 Tree Height = Root Heigth A Depth of C = 1 B Depth = Level = 1 r Path length = # edge in path C = 4 Height of C = 3 D Height of D = ? = 1 Depth = Level = 2 E F I Depth = Level = 3 J เครอตราช 6 K Empty Tree Height = -1 L Depth = Level = 4 รศ. ดร. บญธร H G รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Ancestor & Decendent Ancestor ����� father of ancestor r A A = ancestor of D if has path from A to D Decendent ������� son of decendent B E A = Proper Ancestor of D if A ≠ D I เครอตราช 7 รศ. กฤตวน ศรบรณ D G H J K L D = Proper Decendent of A if D ≠ A รศ. ดร. บญธร F C KMITL 01076249 Data Structures & Algorithms : Tree 1
Tree Examples รศ. ดร. บญธร เครอตราช 8 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Tree Examples รศ. ดร. บญธร เครอตราช 9 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Thai Royal Family Tree รศ. ดร. บญธร เครอตราช 10 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 11 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Binary Tree bi = 2 has at most 2 subtrees (has 0, 1 or 2 subtrees) r r 2 1 2 4 3 8 10 รศ. ดร. บญธร C 6 11 เครอตราช 12 รศ. กฤตวน ศรบรณ r D 9 p q B 5 7 r 3 A s Normally not draw • branch direction • null ptr KMITL 01076249 Data Structures & Algorithms : Tree 1
Perfect Binary Tree (Ambiguously also called Complete Binary Tree ) Every level is completely filled # Nodes in each level : Depth = Level = 0 D=L=1 2 D=L=3 1=20 1 4 8 5 9 2=21 3 10 6 11 12 4=22 7 13 14 15 8=23 H Tree Height D Leave Depth H =D D = L = 4 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 16=24=2 D N = จำนวน node ใน perfect binary tree N = 20+21+. . . +2 H N = 2 H+1 - 1 N = (2 Hexternal) + (2 H-1 internal) H = D = log 2 (N+1) – 1 O( log 2(N)) รศ. ดร. บญธร เครอตราช 13 รศ. กฤตวน ศรบรณ Perfect Binary Tree of N nodes height H : H = log 2 (N+1) – 1 N = 2 H+1 - 1 KMITL 01076249 Data Structures & Algorithms : Tree 1
Complete Binary. Tree Complete Binary Tree Every level, except possibly the last, is completely filled & All nodes are as far left as possible. 1 1 2 4 8 5 9 2 3 10 6 11 12 4 7 8 Complete binary tree รศ. ดร. บญธร เครอตราช 14 รศ. กฤตวน ศรบรณ 3 5 9 10 6 11 12 Without node 11 : Not a complete binary tree KMITL 01076249 Data Structures & Algorithms : Tree 1 7
Perfect Binary Tree & Complete Binary Tree N จำนวน node และ ความสง Perfect Binary Tree H = log 2 (N+1) – 1 = �log 2 N � → O( log 2(N)) N = 2 H+1 - 1 H Complete Binary Tree H = log 2 (N+1) – 1 = �log 2 N � → O( log 2(N)) = ] N ������� [22 HH+1 , – 1 # Nodes in each level D=L=0 D=L=1 2 D=L=2 H D H 8 9 10 6 11 12 13 14 2 4=22 7 8=23 15 Perfect Binary Tree Every level is completely filled (Ambiguously also called Complete Binary Tree ) 4 2 H =23 = 8 16=24=2 D 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Tree Height Leave Depth =D 5 1 2=21 3 4 D=L=3 D=L=4 1=20 1 N total # of nodes = 20+21+. . . +2 H = 2 H+1 – 1 ���� external 2 H 8 16 3 5 6 9 10 2 H+1 =24 = 16 7 15 2 H+1 -1 = 24 = 15 Complete Binary Tree Every level is completely filled, except possibly the last, & All nodes are as far left as possible internal 2 H-1 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL เครอตราช รศ. กฤตวน ศรบรณ 15 01076249 Data Structures & Algorithms : Tree 1 KMITL 01076249 Data Structures & Algorithms :
Binary Tree & Complete Binary Tree N จำนวน node และ ความสง Binary Tree H Complete Binary Tree H ������� [ N-1 , N-1 log 2 N � ] H = log 2 (N+1) – 1 = �log 2 N � � O(N) ������� [ O(N) , O( log 2(N)) ] H-1 Log 2 1, 000 < 20 → O( log 2(N)) N ������� [2 H, 2 H+1 – 1] 1 H-1 , 2 H+1 N ������� [ – 1 ] H = N-1 2 2 3 4 4 5 Binary Tree Worst Case Height: Linked List O(N) รศ. ดร. บญธร 1 เครอตราช รศ. กฤตวน ศรบรณ KMITL เครอตราช รศ. กฤตวน ศรบรณ 16 2 H =23 = 8 8 16 3 5 6 9 10 2 H+1 =24 = 16 7 15 2 H+1 -1 = 24 = 15 Complete Binary Tree Every level is completely filled, except possibly the last, & All nodes are as far left as possible 01076249 Data Structures & Algorithms : Tree 1 KMITL 01076249 Data Structures & Algorithms :
Full (proper, strictly, 2 -tree) Binary. Tree Full Node = node with 2 children Full binary tree node = full node or leaf edge top A D Proof : = 2 #internal C E H edge bottom = #external + #internal – 1 (root) edge bottom = edge top ∴ #external + #internal – 1 = 2 #internal #external = #internal +1 edge top B edge bottom # external = # internal + 1 edge top r L F I G J K M N Q How good ? P R Try add every leaf with 2 sons. Before : N = 17 See how good? Max Search Path = Tree’s Height รศ. ดร. บญธร เครอตราช 17 รศ. กฤตวน ศรบรณ KMITL Add = 18 more Max Search Path increases only 1 01076249 Data Structures & Algorithms : Tree 1
Complete Binary Tree : H and N How can 10 nodes in a binary tree has shortest search path ? 1 2 3 4 6 5 8 7 9 1 0 Linked list : H = N – 1 1 2 3 4 8 5 9 6 7 1 0 Ideal tree Complete Binary Tree Log 2 1, 000 < 20 รศ. ดร. บญธร เครอตราช 18 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Full Complete Perfect รศ. ดร. บญธร เครอตราช 19 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Binary Search Tree : for every node A r 14 < 4 r 2 ≥ < ≥ ≥ 3 9 18 < < 5 รศ. ดร. บญธร < 16 เครอตราช 20 r 3 A ≥ 16 B ≥ 5 ≥ ≥ C ≥ D 20 รศ. กฤตวน ศรบรณ KMITL 8 < ≥ 15 7 left decendents < A right decendents ≥ A 6 ≥ 7 01076249 Data Structures & Algorithms : Tree 1
Why Tree ? Implicit (Sequential ) Array Insertion – Deletion Problems Linear Linked list Linear Search O(n) UNIX File system of several popular OS Tree Search O(log 2 n) รศ. ดร. บญธร เครอตราช 21 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 22 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Depth – First Order Depth-First Order จาก root ไปดานลกกอน ชวยในการหา ไปลกคนแรก แบงเปนแลวไปหลานคนแรกกอนไปทลกคนท 3 แบบ ขนกบการวางตำแหนงการ ไวทใด Inorder (Symmetric Order) 1. 2. 3. in. Order(left. Subtree) visit_root in. Order(right. Subtree) รศ. ดร. บญธร เครอตราช 25 ไปดานขาง ใช stack visit 2 root Preorder 1. 2. 3. Postorder visit_root pre. Order(left. Subtree) pre. Order(right. Subtree) รศ. กฤตวน ศรบรณ KMITL 1. 2. 3. post. Order(left. Subtree) post. Order(right. Subtree) visit_root 01076249 Data Structures & Algorithms : Tree 1
Inorder (Symmetric Order) in. Order( root ) { if (root){ in. Order(root->left) visit(root) in. Order(root->right) } } ก 2 2 1 1 1 2 D 1 3 1 D 1รศ. ดร. บญธร G เครอตราช 2 26 2 B ข pre 2 3 3 G รศ. กฤตวน 3ศรบรณ ค A B 3 in post 2 1 2 E 1 3 1 A E KMITL C H 3 2 F 1 I 3 H 3 2 C I 3 F 01076249 Data Structures & Algorithms : Tree 1
Preorder pre. Order( root ) { if (root){ visit(root) pre. Order(root->left) pre. Order(root->right) } } ก E G เครอตราช 27 ค C D รศ. ดร. บญธร in post A B A ข pre B รศ. กฤตวน ศรบรณ D G KMITL F H I C E H F I 01076249 Data Structures & Algorithms : Tree 1
Postorder post. Order( root ) { if (root){ post. Order(root->left) post. Order(root->right) visit(root) } } ก E G เครอตราช 28 ค C D รศ. ดร. บญธร in post A B G ข pre D รศ. กฤตวน ศรบรณ B F H H KMITL E I I F C A 01076249 Data Structures & Algorithms : Tree 1
Test Your Self ข pre ก in post Binary Search Tree ค A r C E 3 D F G J I 14 4 B K L 5 15 9 7 H Inorder : Lroot. R 1. in. Order(L) 2. visit_root 3. in. Order(R) Postorder : LRroot 18 16 20 16 Inorder EICFJBGDKHLA 3 4 5 7 9 14 15 16 16 18 20 Preorder ABCEIFJDGHKL 14 4 3 9 7 5 15 18 16 16 20 Postorder IEJFCGKLHDBA 3 5 7 9 4 16 16 20 18 15 14 รศ. ดร. บญธร เครอตราช 29 รศ. กฤตวน ศรบรณ KMITL Preorder : root. LR Asscending Order ! 01076249 Data Structures & Algorithms : Tree 1
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 30 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Binary Search. Tree Binary Search Tree : for every node A r 14 < 4 r 2 ≥ < ≥ ≥ 3 9 18 < < 5 รศ. ดร. บญธร < 16 เครอตราช 31 r 3 A ≥ 16 B ≥ 5 ≥ ≥ C ≥ D 20 รศ. กฤตวน ศรบรณ KMITL 8 < ≥ 15 7 left decendents < A right decendents ≥ A 6 ≥ 7 01076249 Data Structures & Algorithms : Tree 1
Insertion r < 14 4 Inserts : 14 รศ. ดร. บญธร เครอตราช 32 4 ≥ 15 < ≥ ≥ 3 9 18 < < 7 16 ≥ 20 < ≥ 5 16 9 รศ. กฤตวน ศรบรณ 7 15 KMITL 3 18 16 20 5 16 01076249 Data Structures & Algorithms : Tree 1
Delete a : Leaf, Node with only 1 child r 4 3 r 14 15 9 7 5 14 4 18 16 15 3 20 18 7 5 16 16 16 Delete Leaf 20 can be deleted right away, still be a binary search tree Delete a node with only one child Delete 9, replace a subtree at the deleted node รศ. ดร. บญธร เครอตราช 33 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Delete a Node with Both Children : Lazy Deletion 1 r r 3 2 2 12 7 1 17 5 4 11 6 9 8 13 15 10 3 1 18 19 17 13 15 7 20 5 14 16 4 11 14 6 9 8 Delete 12 10 19 20 16 Increases tree height. => Lazy Deletion Where can we put tree 7, and tree 17 ? Can replace only 1 at the deleted node. It’s a binary search tree! รศ. ดร. บญธร 18 เครอตราช 34 Let choose 17. Where to put tree 7 ? Where is it’s place ? รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Delete a Node with Both Children : Lazy Deletion 2 r r 3 2 2 12 7 1 4 13 11 6 9 8 15 10 5 4 18 19 7 1 17 5 3 11 6 9 8 20 14 16 17 10 Increases tree height. => Lazy Deletion 13 15 18 19 20 14 16 Delete 12 Can replace only 1 at the deleted node. If we choose 7. รศ. ดร. บญธร Where is 17’s place ? เครอตราช 35 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Deletion : Using Inorder Successor / Predecessor r 3 2 Replace 12 with data 13 12 7 1 17 5 13 11 6 4 9 8 r 10 2 9 8 รศ. ดร. บญธร 20 Not increasing tree height !!! 11 6 4 10 เครอตราช 36 9 8 13 15 17 11 6 7 1 11 = inorder predecessor of 12 Replace 12 with data 11 Delete 11 instead as 1 child deletion 12 5 12 13 14 16 7 4 Delete 13 instead as 1 child deletion 19 3 2 18 15 3 1 r 13 = inorder successor of 12 r 7 17 รศ. กฤตวน ศรบรณ 11 6 9 8 14 16 KMITL 14 16 12 11 1 4 20 19 3 5 20 18 15 10 2 18 19 13 13 15 10 18 19 14 16 01076249 Data Structures & Algorithms : Tree 1 20
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 37 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Binary Tree Representations 1. Dynamic r typedef int T; struct node{ T data; struct node *left, *right; }; typedef struct node; 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 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 รศ. ดร. บญธร เครอตราช 38 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
(Sequential) Implicit Array H 7 D 3 A 0 B 1 I 8 J 9 E 4 K 10 L 11 F 5 C 2 M 12 N 13 G 6 O 14 H 8 D 4 A 1 B 2 I 9 J 10 E 5 K 11 L 12 F 6 C 3 M 13 N 14 G 7 O 15 A B C D E F G H I J K L M N O 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 D 4 14 8 L M เครอตราช 39 รศ. กฤตวน ศรบรณ B 2 I 9 5 10 A B C D 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 รศ. ดร. บญธร A 1 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 1
(Sequential) Implicit Array D 3 7 A 0 B 1 4 I 8 9 A B C D 10 F G L 11 I F 5 C 2 M 12 D 4 G 6 13 14 L M 8 A 1 B 2 I 9 5 10 A B C D 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Where is the root ? The node at index i ‘s left son ? right son ? father ? 11 L 12 F G I F 6 C 3 M 13 G 7 14 L M 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Start at index 0 0 2 i+1 2 i+2 (i - 1) div 2 Start at index 1 1 2 i 2 i+1 i div 2 So good ! No memory for link ! Easy to calculate ! What happen if we have only node at indices : 0, 2, 6, 14 ? ie. sparse What shape of tree should be best for sequential array? รศ. ดร. บญธร เครอตราช 40 รศ. กฤตวน ศรบรณ KMITL 15 01076249 Data Structures & Algorithms : Tree 1
Tree 1. Tree Definitions 2. Binary Tree – Traversals – Binary Search Tree – Representations – Application : Expression Tree รศ. ดร. บญธร เครอตราช 41 รศ. กฤตวน ศรบรณ KMITL 3. 4. AVL Tree Which Representions ? 5. n-ary Tree 6. Generic Tree 7. Multiway Search Tree 8. B-Trees 01076249 Data Structures & Algorithms : Tree 1
Example : Expression Tree r + + * a * b + * c d g f e (a + b * c) + ((d * e + f) * g) รศ. ดร. บญธร เครอตราช 42 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
Constructing an Expression Tree stack S a b + a a c d c + d b + * b c a d a ab+cde+** e b + + input postfix form + e * b c * d รศ. ดร. บญธร + e เครอตราช 43 e Scan input from left to right. Operand Create an operand node and push to the stack Operator Create an operator node Pop 2 operands to be its children. Push to the stack. รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 1
- Slides: 43