Download as pdf or txt
Download as pdf or txt
You are on page 1of 91

1

CHAPTER SEVEN

BINARY TREES
3/2/2024 DSA- CH-7: Binary trees
CH-7 Contents
2

1. Trees, binary trees and binary search trees


2. Implementing binary trees
3. Binary Search tree operation
1. Insertion
2. Deletion
3. Searching
4. Traversal
4. Balancing a tree

3/2/2024
CH-7 Contents
3

1. Trees, binary trees and binary search trees


2. Implementing binary trees
3. Binary Search tree operation
1. Insertion

2. Deletion

3. Searching

4. Traversal

4. Balancing a tree

3/2/2024
Data Structures: Organizing data
4

 Linked lists: usually provide greater flexibility that


arrays, but they are linear structures and it is
difficult to use them to organized a hierarchical
representation of objects.
 Stack and Queues: reflect some hierarchy, they are
limited to only one dimension.
 To overcome this limitation, we create a new data
type called a tree that consists of nodes and arcs.
 Unlike natural trees are depicted upside down with the
root at the top and the leaves at the bottom.

3/2/2024
Tree
5

 A tree is an abstract data type


 Itis a collection of nodes connected by directed (or
undirected) edges.
◼ Edge: the link from one node to another
A tree is a non-linear data structure, compared to
arrays, linked lists, stacks and queues which are linear
data structures.
 A tree can be empty with no nodes or a tree is a
structure consisting of one node called the root and
zero or one or more sub-trees.

3/2/2024
Example
6

3/2/2024
Properties of Trees and Nodes
7

 Only access point is the root


 One node is distinguished as a root; like A
 All nodes, except the root, have one parent
 Every node (exclude a root) is connected by a directed
edge from exactly one other node; A direction is: parent →
children
◼ A is a parent of B and C
◼ B is called a child of A.
◼ On the other hand, B is a parent of D, E, F

 In the above picture, the root has 2 sub-trees.


 Each node can have arbitrary number of children.
3/2/2024
8

 Nodes with no children are called leaves, or external


nodes.
 In the above picture, D, I, J, F, K, H are leaves.
 Nodes, which are not leaves, are called internal nodes.
 Internal nodes have at least one child.
 Nodes with the same parent are called siblings.
 In the picture, B, C are called siblings.
 The depth of a node is the number of edges from the
root to the node.
 The depth of E is 2.

3/2/2024
9

 The height of a node is the maximum number of


edges of any leaf from this node.
A leaf has a height of 0
 The height of a tree is a height of a root.

 The height of B is 2.

3/2/2024
Important Terms
10

 Path − Path refers to the sequence of nodes along the


edges of a tree.
 Path length: the number of edges that must be traversed to get
from one node to another
 Root – The node at the top of the tree is called root. There
is only one root per tree and one path from the root node to
any node.
 Parent − Any node except the root node has one edge
upward to a node called parent.
 Child – The node below a given node connected by its edge
downward is called its child node.
 Leaf – The node which does not have any child node is
called the leaf node.
3/2/2024
11

 Sub-tree − sub-tree represents the descendants of a


node.
 Visiting − visiting refers to checking the value of a
node when control is on the node.
 Traversing − Traversing means passing through nodes
in a specific order.
 Levels − Level of a node represents the generation of
a node. If the root node is at level 0, then its next child
node is at level 1, its grandchild is at level 2, and so on.
 Keys − Key represents a value of a node based on
which a search operation is to be carried out for a
node.
3/2/2024
Exercise: Given Tree
12

3/2/2024
Exercise
13

1. What is the root node of the tree?


2. How many sub-trees that the root contains?
3. List all the external nodes?
4. What is the depth of the node that contains M?
5. What is the height of the root?
6. What is the height of the node that contains L?

3/2/2024
Binary Tree
14

 A general tree is a tree where each node may have


zero or more children (a binary tree is a
specialized case of a general tree).
 General trees are used to model applications such as
file systems.
 Binary tree: each node has at most two children
 The possible children are usually referred to as the left
child and the right child
 A unique path exists from the root to every other
node

3/2/2024
Binary Tree: # of nodes
15

 What is the MAX # of nodes at some level L?


 The max # of nodes at level L is 2L
◼ Where L=0, 1, 2, … L -1

3/2/2024
Why height (H) is important?
16

 What is the total # nodes N of a full tree with height


H?
 Full Binary Tree: Every node has exactly two children and
all the leaves are on the same level.
 N= 2H + 1 -1

 What is the height H of a full tree with N nodes?


 H= log(N +1) - 1→ O(Log N)
 Why height (H) is important?
 Tree operations (e.g., insert, delete, retrieve etc.) are
typically expressed in terms of H.
 So, H determines running time!

3/2/2024
17

•What is the max height of a tree with N


nodes? N (same as a linked list)

•What is the min height of a tree with N


nodes? log(N+1)

3/2/2024
How to search a binary tree?
18

1. Start at the root


2. Search the tree level by level, until you find the
element you are searching for or you reach a leaf.
 Is this better than searching a linked list?
◼ No → O(N)

3/2/2024
Binary Search Tree (BST)
19

 A data structure for efficient searching, insertion


and deletion.
 Binary search tree property
 For every node X
 All the keys in its left
sub-tree are smaller than
the key value in X
 All the keys in its right
sub-tree are larger than the
key value in X
3/2/2024
BST Representation
20

 Binary Search tree exhibits a special behavior.


A node's left child must have a value less than its
parent's value and the node's right child must have a
value greater than its parent value.

3/2/2024
CH-7 Contents
21

1. Trees, binary trees and binary search trees


2. Implementing binary trees
3. Binary Search tree operation
1. Insertion

2. Deletion

3. searching

4. Traversal

4. Balancing a tree

3/2/2024
Implementing Binary Tree
22

 A tree is represented by a pointer to the topmost


node in tree. If the tree is empty, then value of root
is NULL.
A tree node contains following parts
◼ Data
◼ Pointer to left child
◼ Pointer to right child

 It can be implemented in at least two ways:


 Array
 Linked List

3/2/2024
Array (Sequential) Representation
23

 To represent tree using array, numbering of nodes


can start either from 0 to (n-1) or 1 to n .

 First case(0 to n-1)  Second case(1 to n)


 if (say) Parent=p;  if (say) Parent=p;

 Left child=(2*p)+1  Left child=(2*p);

 Right child=(2*p)+2;  Right child=(2*p)+1;


3/2/2024
Limitation of array implementation
24

 This implementation may be inconvenient, as is often


the case with static allocation,
 Sincethe size of the array has to become part of the
program and must known in advance.
 It is a problem because the data may overflow the
array if too little space is allocated, or memory
space may be wasted if too much space is
allocated.

3/2/2024
Linked Representation: Dynamic Node
25

 A node is declared as a linked list with an


information field and two pointer fields.
struct node
{
int data;
struct node *left;
struct node *right;
};

3/2/2024
CH-7 Contents
26

1. Trees, binary trees and binary search trees


2. Implementing binary trees
3. Binary Search tree operation
1. Insertion

2. Deletion

3. searching

4. Traversal

4. Balancing a tree

3/2/2024
BST Basic Operations
27

 The basic operations that can be performed on a


binary search tree data structure, are the following
 Search − Searches an element in a tree.
 Insert − Inserts an element in a tree/create a tree.
 Delete− Deletes an element from a tree
 Traversal
◼ Pre-order Traversal − Traverses a tree in a pre-order
manner.
◼ In-order Traversal − Traverses a tree in an in-order manner.
◼ Post-order Traversal − Traverses a tree in a post-order
manner.

3/2/2024
Searching: BST
28

 For every node X


 All the keys in its left sub-tree are smaller than
the key value in X
 All the keys in its right sub-tree are larger than
the key value in X
 Example: search for15

3/2/2024
29 3/2/2024
Searching Program: BST
30

 Time complexity: O(height of the tree)


3/2/2024
Finding MIN or MAX
31

 Goal: return the node containing the smallest


(largest) key in the tree
 Algorithm: Start at the root and go left (right) as
long as there is a left (right) child.
 The stopping point is the smallest (largest) element

3/2/2024
 Time complexity: O(height of the tree)
Insertion: BST
32

 Proceed down the tree as you would with a find


 If X is found, do nothing (or update something)
 Otherwise, insert X at the last spot on the path
traversed

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

 When we delete a node, we need to consider how


we take care of the children of the deleted node.
 Thishas to be done such that the property of the
search tree is maintained.
 Deletion under different cases
 Case 1: the node is a leaf
 Case 2: the node has one child

 Case 3: the node has 2 children

3/2/2024
Case 1
37

 Case 1: the node is a leaf


 Delete it immediately

3/2/2024
Case 2
38

 Case 2: the node has one child


 Adjust a pointer from the parent to bypass that node

3/2/2024
Case 3
39

 Case 3: the node has 2 children


 Replace the key of that node with the minimum element
at the right sub-tree
 Delete that minimum element
◼ Has either no child or only right child because if it has a left
child, that left child would be smaller and would have been
chosen. So invoke case 1 or 2.

3/2/2024
Traversal: Binary Search Tree
40

 Many algorithms require all nodes of a binary


tree be visited and the contents of each node
processed or examined.
 There are 4 traditional types of traversals
 Preorder traversal: process the root, then process all sub trees
(left to right)
 In order traversal: process the left sub tree, process the root,
process the right sub tree
 Post order traversal: process the left sub tree, process the right
sub tree, then process the root
 Level order traversal: starting from the root of a tree, process all
nodes at the same depth from left to right, then proceed to the
nodes at the next depth

3/2/2024
Result of Traversal
41

 To determine the results of a traversal on a given


tree draw a path around the tree.
 Start on the left side of the root and trace around the
tree. The path should stay close to the tree

3/2/2024
42

 Pre order: process when pass down left side of


node: 12 49 13 5 42
 In order: process when pass underneath node:

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

void preOrder(treePointer ptr)


{
(ptr != NULL)
{
visit(t);
preOrder(ptr->leftChild);
preOrder(ptr->rightChild);
}
}
3/2/2024
Algorithm: In order Traversal
44

void inOrder(treePointer ptr)


{
(ptr != NULL)
{
inOrder(ptr->leftChild);
visit(t);
inOrder(ptr->rightChild);
}
}
3/2/2024
Algorithm: Post order Traversal
45

void postOrder(treePointer ptr)


{
(ptr != NULL)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
visit(t);
}
}
3/2/2024
Algorithm: Level order Traversal
46

Let ptr be a pointer to the tree


root.
while (ptr != NULL)
{
visit node pointed at by ptr and
put its children on a FIFO queue;
if FIFO queue is empty, set ptr =
NULL;
otherwise, delete a node from the
FIFO queue and call it ptr;
} 3/2/2024
Traversal Application
47

 Make a clone
 Determine height
 Determine number of nodes

3/2/2024
Example: BST Traversal
48

 What is the result of


a given traversal type
 Pre order
 In order

 Post order

 Level order

3/2/2024
CH-7 Contents
49

1. Trees, binary trees and binary search trees


2. Implementing binary trees
3. Binary Search tree operation
1. Insertion

2. Deletion

3. searching

4. Traversal

4. Balancing a tree

3/2/2024
Review: Constructing a BST
50

 BSTs where introduced because in theory they give


nice fast search time.
 Example1: : Design a BST using the following sequence
of data: 6, 4, 8, 5, 7, 3, 9,10
 Example2: : Design a BST using the following sequence
of data: 3, 4, 5, 6, 7, 8 ,9 ,10
 We have seen that depending on how the data
arrives the tree can degrade into a linked list
 So what is a good programmer to do.
 Of course, they are to balance the tree.

10/23/2017
Balancing a tree
51

 A binary tree is height balanced or simply


balanced if the difference in height of both
sub-trees of any node is the tree is either zero
or one.
 A tree is considered perfectly balanced if it is
balanced and all leaves are to be found on one
level or two levels.

10/23/2017
Example 1: BST with the same info
52

10/23/2017
Techniques : Balancing a Tree
53

 There are a number of techniques to properly


balance a binary search tree.
 Some of them consist of reordering the data
themselves and then building a tree, if an ordering of
the guarantees that the resulting tree is balanced.
 Some of them consist of constantly reconstructing the
tree when elements arrive and lead to an unbalanced
tree.

10/23/2017
Technique1: Sort and Construct the tree
54

 When data arrive, store all of them in an array. If


all possible data arrived , sort the array using one
of the efficient algorithm discussed in chapter 3.
 Now, designate for the root the middle element in
the array.
 The array now consists of two sub-arrays:
1. Between the beginning of the array and the element
just chosen for the root and
2. Between the root and the end of the array.

10/23/2017
55

 The left child of the root is taken from the middle of


the first sub-array, its right child an element in the
middle of the second.
 Now, building the level containing the children of the
root is finished.
 The next level, with children of children of the root,
is constructed in the same fashion using four sub-
arrays and the middle elements from each of them.

10/23/2017
Example 2: Constructing a BST
56

 Example : Design a BST using the following


sequence of data: 10, 8, 9, 6, 7, 4, 5, 3
 Step 1: Store the elements in an array
 Step 2: Sort the array
 Step 3: Peek the middle element
 Mid= (left index + right index) /2
 Step 4: Repeat for the sub arrays
 Step 5: Stop , if sub-array contains 0 element

10/23/2017
Exercise 1: Constructing a BST
57

 Design a BST using the following stream of


data: 5 1 9 8 7 0 2 3 4 6

10/23/2017
Technique 1: Drawback
58

 This algorithm has one serious drawback:


 Required an additional array which needed to be sorted
before the construction of a perfectly balanced tree
began.
◼ All data must be put in an array before the tree can be created.
◼ They can be stored in the array directly from the input.
 To avoid sorting, it required deconstructing and then
reconstructing the tree, which is inefficient except for
relatively small trees.
◼ The data can be transferred from an unbalanced tree to the
array using in-order traversal.
◼ The tree can now be deleted and re-created.

10/23/2017
Technique 2: The DSW Algorithm
59

 Devised by Colin Day and later improved by


then for Quentin F. Stout and Bette L. Warren,
hence DSW.
 The building block for tree transformation in this
algorithm is rotation.
◼ Rotation is the process of moving the nodes to either
left or right to make tree balanced.
 There are two types of rotation: left and right,
which are symmetrical to one another.
10/23/2017
Left Rotation
60

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

 So the idea is to take a tree and perform some


rotations to it to make it balanced.
1. First you create a backbone or a vine
◼ An arbitrary search tree into a linked list like tree called
backbone or vine.
2. Then you transform the backbone into a nicely
balanced tree

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

 In the best case, when the tree is already a Backbone,


the while loop is executed n times and no rotation is
performed.
 In the worst case, when the root does not have a right
child, the while loop executes 2n -1 times n-1 rotation
performed, where n is the number of nodes in the tree;
that is the run time of the first phase is O(n).
 In this case, for each node except the one with the smallest
value, the left child of temp is rotated about temp. After all
rotation are finished, temp points to the root, and after n
iterations, it descends down the backbone to become null.

10/23/2017
Example 3: Transforming a BST into a
66
backbone

10/23/2017
Creating a balanced tree
67

 In each pass down the backbone, every second


node down to a certain point is rotated about it
parent.
 The first pass is used to account for the different
between the number n of nodes in the current tree
and the number 2 [log n +1] -1 of nodes in the closest
complete binary tree where [ x ] is the closest
integer less than x.
 That is, the overflowing modes are treated separately.

10/23/2017
Example 4: Creating a Balanced Tree
68

 Create a balanced tree for the previous created


backbone?
 Step 1: calculate M= 2[log(n+1)]-1;
◼ M= 2[log(9+1)]-1 =7
 Step 2: Make n- M rotation starting from the top of the
backbone
◼ n –M = 2 rotation
 Step 3: calculate M= M/2
◼ M= M/2 → 7/2, (7/2)/2, …
 Step 4: Make M rotation starting from the top of the
backbone
 Step 5: repeat until M <= 1

10/23/2017
Solution: Example 4
69


10/23/2017
70

 To compute the complexity of the tree building


phase, observe that the number of iteration
performed by the while loop equals

 The number of rotation can given by the formula

10/23/2017
71

 That is , the number of rotation is O(n). Because creating a


backbone also required at most O(n) rotations.
 The cost of global rebalancing with the DSW algorithm is
optimal in terms of time because it grows linearly with n
and requires a very small and fixed amount of storage.
 The DSW algorithm is good if you can take the time to
get all the nodes and then create the tree
 What if you want to balance the tree as you go?
 You use an AVL Tree

10/23/2017
AVL Tree
72

 An AVL tree is another balanced binary search tree.


 Named after their inventors, Adelson-Velskii and Landis,
they were the first dynamically balanced trees to be
proposed.
 Insertion, deletion and searching operations also take
O(logn) time.
 An AVL tree is a binary search tree which has the
following properties:
 The sub-trees of every node differ in height by at most one.
 Every sub-tree is an AVL tree.

10/23/2017
Balance Factor
73

 Balance factor of a node is the difference between


the heights of left and right sub-trees of that node.
 The balance factor of a node is calculated either
 Balance factor= height (left subtree) – height(right subtree)
 Balance factor= height (right subtree) – height(left subtree)
 In an AVL tree, balance factor of every node is either
-1, 0 or +1.

10/23/2017
Example 5: AVL Tree
74

 The above tree is a binary search tree and every node is


satisfying balance factor condition. So this tree is said to be
an AVL tree. 10/23/2017
AVL Tree Rotations
75

 In AVL tree, after performing every operation like


insertion and deletion we need to check the
balance factor of every node in the tree.
 If every node satisfies the balance factor condition
then we conclude the operation otherwise we must
make it balanced.
 We use rotation operations to make the tree
balanced whenever the tree is becoming
imbalanced due to any operation.

10/23/2017
Type of Rotation: AVL Rotation
76

 There are four rotations and they are classified into


two types

10/23/2017
Single Left Rotation (LL Rotation)
77

 In LL Rotation every node moves one position to left


from the current position.
 To understand LL Rotation, let us consider following
insertion operations into an AVL Tree...

10/23/2017
Single Right Rotation (RR Rotation)
78

 In RR Rotation every node moves one position to


right from the current position.
 To understand RR Rotation, let us consider following
insertion operations into an AVL Tree...

10/23/2017
Left Right Rotation (LR Rotation)
79

 The LR Rotation is combination of single left rotation followed


by single right rotation. In LR Rotation, first every node moves
one position to left then one position to right from the current
position.
 To understand LR Rotation, let us consider following insertion
operations into an AVL Tree...

10/23/2017
Right Left Rotation (RL Rotation)
80

 The RL Rotation is combination of single right rotation followed


by single left rotation. In RL Rotation, first every node moves
one position to right then one position to left from the current
position.
 To understand RL Rotation, let us consider following insertion
operations into an AVL Tree...

10/23/2017
Search Operation: AVL Tree
81

 In an AVL tree, the search operation is performed


with O(log n) time complexity.
 The search operation is performed similar to Binary
search tree search operation.
 How?

10/23/2017
Insertion Operation: AVL Tree
82

 In an AVL tree, the insertion operation is performed with


O(log n) time complexity.
 In AVL Tree, new node is always inserted as a leaf node. The
insertion operation is performed as follows
 Step 1: Insert the new element into the tree using Binary Search
Tree insertion logic.
 Step 2: After insertion, check the Balance Factor of every node.
 Step 3: If the Balance Factor of every node is 0 or 1 or -1 then
go for next operation.
 Step 4: If the Balance Factor of any node is other than 0 or 1 or -
1 then tree is said to be imbalanced. Then perform the suitable
Rotation to make it balanced. And go for next operation.

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

 Construct an AVL Tree by inserting numbers from 8


to 1.
 Show the state of the AVL Tree for each insertion
operation.

10/23/2017
Deletion Operation: AVL Tree
88

 In an AVL Tree, the deletion operation is similar to


deletion operation in BST.
 But after every deletion operation we need to check
with the Balance Factor condition.
 If the tree is balanced after deletion then go for next
operation otherwise perform the suitable rotation to
make the tree Balanced.

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

You might also like