Binary Tree
1
2
The above tree is a binary tree because each node contains the atmost two children. The logical representation of the above tree is given below:
3
4
Properties of Binary Tree
5
6
7
Types of Binary Tree
8
Full/ proper/ strict Binary tree
9
Properties of Full Binary Tree
10
11
12
The above tree is a complete binary tree because all the nodes are completely filled, and all the nodes in the last level are added at the left first.
13
14
15
Degenerate Binary Tree
16
17
18
Balanced Binary Tree
19
20
21
The above tree is not a balanced binary tree because the difference between the left subtree and the right subtree is greater than 1.
Create Root
22
23
Inserting into a Tree
24
25
26
27
Traversing a Tree
28
Tree Traversal Algorithms
29
In-order Traversal
30
31
32
33
34
35
36
Pre-order Traversal
37
38
39
40
Post-order Traversal
41
42
43
44
Why Binary Search Trees?
45
Binary Search Tree Property
46
Example
47
Example: The left tree is a binary search tree and the right tree is not a binary search tree (at node 6 it's not satisfying the binary search tree property).
Binary Search Tree Declaration
48
BST Declaration
class BSTNode:
def _init_ (self, data):
self.data = data
self.left= None
self. right = None
#set data
def setdata(self, data):
self.data = data
#get data
def getData(self):
return self.data
49
#get left child of a node
def getLeft(self):
return self.left
#get right child of a node
def getRight(self):
return self.right
50
Operations on Binary Search Trees
51
Finding an Element in Binary Search Trees
52
def find(root,data):
currentNode=root
while currentNode is not None and data!=currentNode.getData()
if data>currentNode.getData():
currentNode=currentNode.getRight()
else:
currentNode=currentNode.getLeft()
return currentNode
Time Complexity: O(n), in worst case (when BST is a skew tree). Space Complexity: O(n), for recursive stack.
53
Finding Minimum Element in Binary Search Trees
54
Finding Minimum Element in Binary Search Trees
def findMin(root):
currentNode=root
if currentNode.getLeft()==None:
return currentNode
else:
return findMin(currentNode.getLeft())
55
Non recursive version
def findMin(root):
currentNode=root
if currentNode.getLeft()==None:
return None
while currentNode.getLeft()!=None:
currentNode=currentNode.getLeft()
return currentNode
Time complexity is O(n) and Space complexity is O(1)
56
Finding Maximum Element in Binary Search Trees
57
Finding Maximum Element in Binary Search Trees
def findMax(root):
currentNode=root
if currentNode.getRight()==None:
return currentNode
else:
return findMax(currentNode.getRight())
58
Non recursive version
def findMax(root):
currentNode=root
if currentNode.getRight()==None:
return None
while currentNode.getRight()!=None:
currentNode=currentNode.getRight()
return currentNode
Time complexity is O(n) and Space complexity is O(1)
59
Inserting an Element in Binary Search Tree
60
Inserting an Element from Binary Search Tree
61
Inserting an Element in Binary Search Tree
def insertNode(root,node):
if root is None:
root = node
else:
if root.data>node.data:
if root.left==None:
root.left=node
else:
insertNode(root.left,node)
else:
if root.right==None:
root.right=node
else:
insertNode(root.right,node)
Time Complexity:O(n). Space Complcxity:O(n). for recursive stack. For iterative version, space complexity is 0(1).
62
Deleting an Element from Binary Search Tree
63
Deleting an Element from Binary Search Tree
64
Deleting an Element from Binary Search Tree
65
Deleting an Element from Binary Search Tree
66
Deleting an Element from Binary Search Tree
67
AVL (Adelson-Velskii and Landis) Trees
68
AVL (Adelson-Velskii and Landis) Trees
Properties of AVL Trees
69
Example
70
As an example, among the above binary search trees, the left one is not an AVL tree, whereas the right binary search tree is an AVL tree.
AVL Tree Declaration
class AVLNode:
def _init_(self,balancefactor,left,right):
self.data=data
self.balancefactor=0
self.left=left
self.right=right
71
Finding height of AVL Tree
def height(self):
return self.recHeight(self.root)
def recHeight(self,root):
if root==None:
return 0
else:
leftH=self.recHeight(r.left)
rightH=self.recHeight(r.right)
if leftH>rightH:
return 1+leftH
else:
retun 1+rightH
Time complexity is O(1)
72
Inserting an Element in AVL Tree
def insertNode(root,node):
if root is None:
root = node
else:
if root.data>node.data:
if root.left==None:
root.left=node
else:
insertNode(root.left,node)
73
Inserting an Element in AVL Tree
else:
if root.right==None:
root.right=node
else:
insertNode(root.right,node)
Time Complexity:O(n).
Space Complcxity:O(n). for recursive stack.
For iterative version, space complexity is 0(1).
74
Rotations
75
Rotations
76
Types of Violations
1. An insertion into the left subtree of the left child of X.
2. An insertion into the right subtree of the left child of X.
3. An insertion into the left subtree of the right child of X.
4. An insertion into the right subtree of the right child of X.
77
78
Single Rotations
79
Single Rotations
80
For example, after the insertion of 7 in the original AVL tree on the left, node 9 becomes unbalanced.
So, we do a single left-left rotation at 9. As a result we get the tree on the right.
def singleLeftRotate(self,root):
w=root.left
root.left=w.right
w.right=root
return w
Time complexity is O(1)
Space complexity is O(1)
81
Right Right Rotation (RR Rotation)
82
For example, after the insertion of 29 in the original AVL tree on the left, node 15 becomes unbalanced.
So, we do a single right-right rotation at 15. As a result we get the tree on the right.
def singleRightRotate(self,root):
w=root.right
root.right=w.left
w.left=root
return w
Time complexity is O(1)
Space complexity is O(1)
83
Double Rotations
84
x=root.left
if x.balanceFactor==-1:
root.balanceFactor=0
x.balanceFactor=0
root=self.singleLeftRotate(root)
else:
y=x.right
if y.balanceFactor==-1:
root.balanceFactor=1
x.balanaceFactor=0
85
elif y.balanceFactor==0
root.balanaceFactor=0
x.balanceFactor=0
else:
root.balanceFactor=0
x.balanceFactor=-1
y.balanceFactor=0
root.left=self.singleRightRotate(x)
root=self.singleLeftRotate(root)
return root
86
Right Left Rotation (RL Rotation)
87
x=root.right
if x.balanceFactor==-1:
root.balanceFactor=0
x.balanceFactor=0
root=self.singleRightRotate(root)
else:
y=x.left
if y.balanceFactor==-1:
root.balanceFactor=1
x.balanaceFactor=0
88
y.balanceFactor==0
root.balanaceFactor=0
x.balanceFactor=0
else:
root.balanceFactor=0
x.balanceFactor=-1
y.balanceFactor=0
root.right=self.singleLeftRotate(x)
root=self.singleRightRotate(root)
return root
89
Heap
90
Create a Heap
91
92
93
94
Inserting into heap
95
96
97
Removing from heap
98
99
Replacing in a Heap
100
101
Multi way search trees
102
103
104
The structure of a node of an m-Way tree is given
105
Searching in an m-Way search tree
106
107
108
# Searches the node
109
110
search():
111
112
113
114