Download presentation
Presentation is loading. Please wait.
1
UNIT III TREES
2
Trees Data Structures Tree Binary tree Nodes
Each node can have 0 or more children A node can have at most one parent Binary tree Tree with 0–2 children per node Tree Binary Tree
3
Trees Terminology Root no parent Leaf no child Interior non-leaf
Height distance from root to leaf Root node Height Interior nodes Leaf nodes
4
Binary Search Trees Key property Value at node Example
Smaller values in left subtree Larger values in right subtree Example X > Y X < Z X Y Z
5
Not a binary search tree
Binary Search Trees Examples 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Not a binary search tree Binary search trees
6
Binary Tree Implementation
Class Node { int data; // Could be int, a class, etc Node *left, *right; // null if empty void insert ( int data ) { … } void delete ( int data ) { … } Node *find ( int data ) { … } … }
7
Iterative Search of Binary Tree
Node *Find( Node *n, int key) { while (n != NULL) { if (n->data == key) // Found it return n; if (n->data > key) // In left subtree n = n->left; else // In right subtree n = n->right; } return null; Node * n = Find( root, 5);
8
Recursive Search of Binary Tree
Node *Find( Node *n, int key) { if (n == NULL) // Not found return( n ); else if (n->data == key) // Found it else if (n->data > key) // In left subtree return Find( n->left, key ); else // In right subtree return Find( n->right, key ); } Node * n = Find( root, 5);
9
Example Binary Searches
Find ( root, 2 ) root 10 5 10 > 2, left 5 > 2, left 2 = 2, found 5 > 2, left 2 = 2, found 5 30 2 45 2 25 45 30 10 25
10
Example Binary Searches
Find (root, 25 ) 10 5 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found 5 30 2 45 2 25 45 30 10 25
11
Degenerate binary tree
Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children more formal definitions exist, above are intuitive ideas Degenerate binary tree Balanced binary tree Complete binary tree
12
Binary Trees Properties
Balanced Height = O( log(n) ) for n nodes Useful for searches Degenerate Height = O(n) for n nodes Similar to linked list Degenerate binary tree Balanced binary tree
13
Binary Search Properties
Time of search Proportional to height of tree Balanced binary tree O( log(n) ) time Degenerate tree O( n ) time Like searching linked list / unsorted array
14
Binary Search Tree Construction
How to build & maintain binary trees? Insertion Deletion Maintain key property (invariant) Smaller values in left subtree Larger values in right subtree
15
Binary Search Tree – Insertion
Algorithm Perform search for value X Search will end at node Y (if X not in tree) If X < Y, insert new leaf X as new left subtree for Y If X > Y, insert new leaf X as new right subtree for Y Observations O( log(n) ) operation for balanced tree Insertions may unbalance tree
16
Example Insertion Insert ( 20 ) 10 10 < 20, right 30 > 20, left
Insert 20 on left 5 30 2 25 45 20
17
Binary Search Tree – Deletion
Algorithm Perform search for value X If X is a leaf, delete X Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation O( log(n) ) operation for balanced tree Deletions may unbalance tree
18
Example Deletion (Leaf)
Delete ( 25 ) 10 10 10 < 25, right 30 > 25, left 25 = 25, delete 5 30 5 30 2 25 45 2 45
19
Example Deletion (Internal Node)
Delete ( 10 ) 10 5 5 5 30 5 30 2 30 2 25 45 2 25 45 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf
20
Example Deletion (Internal Node)
Delete ( 10 ) 10 25 25 5 30 5 30 5 30 2 25 45 2 25 45 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree
21
Tree Traversal Goal: visit every node of a tree in-order traversal
+ % A * * 4 / D 5 C 5 Goal: visit every node of a tree in-order traversal void Node::inOrder () { if (left != NULL) { cout << “(“; left->inOrder(); cout << “)”; } cout << data << endl; if (right != NULL) right->inOrder() } Output: A – C / 5 * 2 + D * 5 % 4 To disambiguate: print brackets
22
Tree Traversal (contd.)
+ % A * * 4 / D 5 C 5 pre-order and post-order: void Node::preOrder () { cout << data << endl; if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); } Output: + - A * / C 5 2 % * D 5 4 void Node::postOrder () { if (left != NULL) left->preOrder (); if (right != NULL) right->preOrder (); cout << data << endl; } Output: A C 5 / 2 * - D 5 * 4 % +
23
AVL Trees AVL trees are height-balanced binary search trees
Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node
24
Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basis N(0) = 1, N(1) = 2 Induction N(h) = N(h-1) + N(h-2) + 1 Solution (recall Fibonacci analysis) N(h) > h ( 1.62) h h-2 h-1
25
Height of an AVL Tree N(h) > h ( 1.62)
Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) n > h hence log n > h (relatively well balanced tree!!) h < 1.44 log2n (i.e., Find takes O(logn))
26
Node Heights 6 6 4 9 4 9 1 5 1 5 8 Tree A (AVL) Tree B (AVL)
height=2 BF=1-0=1 2 6 6 1 1 1 4 9 4 9 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1
27
Node Heights after Insert 7
Tree A (AVL) Tree B (not AVL) balance factor 1-(-1) = 2 2 3 6 6 1 1 1 2 4 9 4 9 -1 1 1 5 7 1 5 8 7 height of node = h balance factor = hleft-hright empty height = -1
28
Insert and Rotation in AVL Trees
Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node
29
Single Rotation in an AVL Tree
2 2 6 6 1 2 1 1 4 9 4 8 1 1 5 8 1 5 7 9 7
30
Insertions in AVL Trees
Let the node that needs rebalancing be . There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . 2. Insertion into right subtree of right child of . Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . 4. Insertion into left subtree of right child of . The rebalancing is performed through four separate rotation algorithms.
31
AVL Insertion: Outside Case
j Consider a valid AVL subtree k h Z h h X Y
32
AVL Insertion: Outside Case
j Inserting into X destroys the AVL property at node j k h Z h+1 h Y X
33
AVL Insertion: Outside Case
j Do a “right rotation” k h Z h+1 h Y X
34
Single right rotation j Do a “right rotation” k h Z h+1 h Y X
35
Outside Case Completed
k “Right rotation” done! (“Left rotation” is mirror symmetric) j h+1 h h X Z Y AVL property has been restored!
36
AVL Insertion: Inside Case
j Consider a valid AVL subtree k h Z h h X Y
37
AVL Insertion: Inside Case
j Inserting into Y destroys the AVL property at node j Does “right rotation” restore balance? k h Z h h+1 X Y
38
AVL Insertion: Inside Case
k “Right rotation” does not restore balance… now k is out of balance j h X h h+1 Z Y
39
AVL Insertion: Inside Case
j Consider the structure of subtree Y… k h Z h h+1 X Y
40
AVL Insertion: Inside Case
j Y = node i and subtrees V and W k h Z i h h+1 X h or h-1 V W
41
AVL Insertion: Inside Case
j We will do a left-right “double rotation” . . . k Z i X V W
42
Double rotation : first rotation
j left rotation complete i Z k W V X
43
Double rotation : second rotation
j Now do a right rotation i Z k W V X
44
Double rotation : second rotation
right rotation complete Balance has been restored i k j h h h or h-1 V Z X W
45
Implementation balance (1,0,-1) key left right
No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don’t perform rotations Once you have performed a rotation (single or double) you won’t need to go back up the tree
46
Single Rotation RotateFromRight(n : reference node pointer) {
p : node pointer; p := n.right; n.right := p.left; p.left := n; n := p } n X You also need to modify the heights or balance factors of n and p Y Z Insert
47
Double Rotation Implement Double Rotation in two lines.
DoubleRotateFromRight(n : reference node pointer) { ???? } n X Z V W
48
Insertion in AVL Trees Insert at the leaf (as for all BST)
only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft-hright) is 2 or –2, adjust tree by rotation around the node
49
Insert in BST Insert(T : reference tree pointer, x : element) : integer { if T = null then T := new tree; T.data := x; return 1;//the links to //children are null case T.data = x : return 0; //Duplicate do nothing T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x); endcase }
50
Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then {T := new tree; T.data := x; height := 0; return;} case T.data = x : return ; //Duplicate do nothing T.data > x : Insert(T.left, x); if ((height(T.left)- height(T.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : Insert(T.right, x); code similar to the left case Endcase T.height := max(height(T.left),height(T.right)) +1; return; }
51
Example of Insertions in an AVL Tree
2 20 Insert 5, 40 1 10 30 25 35
52
Example of Insertions in an AVL Tree
2 3 20 20 1 1 1 2 10 30 10 30 1 5 25 35 5 25 35 40 Now Insert 45
53
Single rotation (outside case)
3 3 20 20 1 2 1 2 10 30 10 30 2 1 5 25 35 5 25 40 35 45 40 1 Imbalance 45 Now Insert 34
54
Double rotation (inside case)
3 3 20 20 1 3 1 2 10 30 10 35 2 1 1 5 Imbalance 25 40 5 30 40 25 34 1 45 35 45 Insertion of 34 34
55
Advanced Trees (Part I)
Multiway Search Trees An m-way search tree is a tree in which, for some integer m called the order of the tree, each node has at most m children. If k <= m is the number of children, then all the nodes contain exactly k-1 keys, which partition all the keys into k subsets consisting of all the keys less than the first key in the node, all the keys between a pair of keys in the node, and all keys greater than the largest key in the node. Advanced Trees (Part I)
56
Example Multiway Search Tree
Advanced Trees (Part I)
57
Balanced Multiway Trees (B-Trees)
A B-tree of order m is an m-way search tree in which All leaves are on the same level. All internal nodes except the root have at most m non-empty children, and at least ceil(m/2) non-empty children. The number of keys in each internal node is one less than the number of its non-empty children, and these keys partition the keys in the children in the fashion of a search tree. The root has at most m children, but may have as few as 2 if it is not a leaf, or none if the tree consists of the root alone. Advanced Trees (Part I)
58
Advanced Trees (Part I)
Example B tree Advanced Trees (Part I)
59
Advanced Trees (Part I)
Why Use B-Trees? Remember that performance is related to the height of the tree We want to minimize the height of the tree Used to process external records (information too large to put into memory), minimizes number of accesses to secondary peripheral Advanced Trees (Part I)
60
Advanced Trees (Part I)
2-3 Search Trees B-Tree of degree 3 Balanced search tree Either the tree is empty or T has two children: root contains 1 data item r is greater than each value in left subtree r is less than each value in right subtree T is of the form: root, left subtree, middle subtree, right subtree r has two data items smaller value in r is greater than everything in left subtree and smaller than everything in middle subtree larger value in r is greater than everything in middle subtree and small than everything in right subtree Advanced Trees (Part I)
61
Advanced Trees (Part I)
Example 50, 90 , 150 , , , Nodes with 2 children must have 1 item Nodes with 3 children must have 2 items Leaves may contain 1 or 2 items Advanced Trees (Part I)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.