DSA Chapter 7
DSA Chapter 7
CHAPTER SEVEN
BINARY TREES
3/2/2024 DSA- CH-7: Binary trees
CH-7 Contents
2
3/2/2024
CH-7 Contents
3
2. Deletion
3. Searching
4. Traversal
4. Balancing a tree
3/2/2024
Data Structures: Organizing data
4
3/2/2024
Tree
5
3/2/2024
Example
6
3/2/2024
Properties of Trees and Nodes
7
3/2/2024
9
The height of B is 2.
3/2/2024
Important Terms
10
3/2/2024
Exercise
13
3/2/2024
Binary Tree
14
3/2/2024
Binary Tree: # of nodes
15
3/2/2024
Why height (H) is important?
16
3/2/2024
17
3/2/2024
How to search a binary tree?
18
3/2/2024
Binary Search Tree (BST)
19
3/2/2024
CH-7 Contents
21
2. Deletion
3. searching
4. Traversal
4. Balancing a tree
3/2/2024
Implementing Binary Tree
22
3/2/2024
Array (Sequential) Representation
23
3/2/2024
Linked Representation: Dynamic Node
25
3/2/2024
CH-7 Contents
26
2. Deletion
3. searching
4. Traversal
4. Balancing a tree
3/2/2024
BST Basic Operations
27
3/2/2024
Searching: BST
28
3/2/2024
29 3/2/2024
Searching Program: BST
30
3/2/2024
Time complexity: O(height of the tree)
Insertion: BST
32
3/2/2024
Time complexity: O(height of the tree)
Example: Insert “64”
33
3/2/2024
34
3/2/2024
35
3/2/2024
Deletion: BST
36
3/2/2024
Case 1
37
3/2/2024
Case 2
38
3/2/2024
Case 3
39
3/2/2024
Traversal: Binary Search Tree
40
3/2/2024
Result of Traversal
41
3/2/2024
42
13 49 5 12 42
Post order: process when pass up right side of
node: 13 5 49 42 12
3/2/2024
Algorithm: Preorder Traversal
43
Make a clone
Determine height
Determine number of nodes
3/2/2024
Example: BST Traversal
48
Post order
Level order
3/2/2024
CH-7 Contents
49
2. Deletion
3. searching
4. Traversal
4. Balancing a tree
3/2/2024
Review: Constructing a BST
50
10/23/2017
Balancing a tree
51
10/23/2017
Example 1: BST with the same info
52
10/23/2017
Techniques : Balancing a Tree
53
10/23/2017
Technique1: Sort and Construct the tree
54
10/23/2017
55
10/23/2017
Example 2: Constructing a BST
56
10/23/2017
Exercise 1: Constructing a BST
57
10/23/2017
Technique 1: Drawback
58
10/23/2017
Technique 2: The DSW Algorithm
59
1. temp = p→ right
2. p→right = temp →left
3. temp→left = p
4. p = temp
Before After
10/23/2017
Right Rotation
61
1. temp = p→ left
2. p→left= temp →right
3. temp→right= p
4. p = temp
Before After
10/23/2017
Rotation: Left and Right
62
10/23/2017
Steps: DSW
63
10/23/2017
Algorithm: DSW
64
createBackbone(root, n ) createPerfectTree(n)
temp = root M = 2[log(n+1)]-1;
While ( temp != 0 ) Make n-M rotations
◼ If temp has a left child starting from the top of
◼ Rotate this child about the backbone;
temp
While ( M > 1 )
◼ Set temp to the child
which just became parent ◼M = M/2;
◼ Else set temp to its right ◼ Make M rotations
child starting from the top of
the backbone;
10/23/2017
Creating a Backbone
65
10/23/2017
Example 3: Transforming a BST into a
66
backbone
10/23/2017
Creating a balanced tree
67
10/23/2017
Example 4: Creating a Balanced Tree
68
10/23/2017
Solution: Example 4
69
→
10/23/2017
70
10/23/2017
71
10/23/2017
AVL Tree
72
10/23/2017
Balance Factor
73
10/23/2017
Example 5: AVL Tree
74
10/23/2017
Type of Rotation: AVL Rotation
76
10/23/2017
Single Left Rotation (LL Rotation)
77
10/23/2017
Single Right Rotation (RR Rotation)
78
10/23/2017
Left Right Rotation (LR Rotation)
79
10/23/2017
Right Left Rotation (RL Rotation)
80
10/23/2017
Search Operation: AVL Tree
81
10/23/2017
Insertion Operation: AVL Tree
82
10/23/2017
Example 6: Construct an AVL Tree by
83
inserting numbers from 1 to 8.
10/23/2017
84 10/23/2017
85 10/23/2017
86
10/23/2017
Exercise 2: Construct an AVL Tree
87
10/23/2017
Deletion Operation: AVL Tree
88
10/23/2017
Exercise 3: Determine the given tree is
89
AVL Tree or not (If not make an AVL Tree)
Yes No
10/23/2017
90
Questions?
3/2/2024
91
Thank You
3/2/2024