Introduction
• A tree is an acyclic graph in which there exists only one path between any pair of
vertices.
• A tree is a graph with minimum edges to connect every vertex.
• If n= number of vertices, then number of edges=n-1.
Tree
• The root of a tree is the node with no parents. There can be at most one root node in a tree.
• An edge refers to the link from parent to child.
• A node with no children is called leaf node (E,J,K,H and I).
• Children of same parent are called siblings (B,C,D are siblings of A, and E,F are the siblings of
B).
• A node p is an ancestor of node q if there exists a path from root to q and p appears on the
path. The node q is called a descendant of p. For example, A,C and G are the ancestors of K.
• The set of all nodes at a given depth is called the level of the tree (B, C and D are the same
level). The root node is at level zero.
• The depth of a node is the length of the path from the root to the node (depth of G is 2, A – C
– G).
Tree
• The height of a node is the length of the path from that node to the deepest
node.
• The height of a tree is the length of the path from the root to the deepest node in
the tree.
• A (rooted) tree with only one node (the root) has a height of zero. In the previous
example, the height of B is 2 (B – F – J).
• If every node in a tree has only one child (except leaf nodes) then we call such
trees skew trees.
• If every node has only left child then we call them left skew trees.
• Similarly, if every node has only right child then we call them right skew trees.
Binary Trees
• A tree is called binary tree if each node has zero child, one child or two children.
• Empty tree is also a valid binary tree.
Types of Binary Trees
• Strict Binary Tree: A binary tree is called strict binary tree if each node has exactly
two children or no children.
Types of Binary Trees
• Full Binary Tree: A binary tree is called full binary tree if each node has exactly
two children and all leaf nodes are at the same level.
Types of Binary Trees
• Complete Binary Tree: Let us assume that the height of the binary tree is h. A
binary tree is called complete binary tree if all leaf nodes are at height h or h – 1
and also without any missing number in the sequence.
Structure of Binary Trees
struct Node {
int data;
struct Node *left;
struct Node *right;
};
Traversal Methods
• Preorder traversal (NLR)
• Inorder traversal (LNR)
• Postorder traversal (LRN)
Preorder Traversal
• Preorder traversal is defined as follows:
• Visit the root.
• Traverse the left subtree in Preorder.
• Traverse the right subtree in Preorder.
void preorder (Node *p)
{ if(p!= Null)
{ printf(“%d” , p data);
preorder (p left);
preorder (p right);
}
}
Preorder Traversal
• The nodes of tree would be visited in the order:
1245367
Inorder Traversal
• In Inorder Traversal the root is visited between the subtrees. Inorder traversal is
defined as follows:
• Traverse the left subtree in Inorder.
• Visit the root.
• Traverse the right subtree in Inorder.
void inorder (Node *p)
{ if(p!= Null)
{ inorder (p left);
printf(“%d” , p data);
inorder (p right);
}
}
Inorder Traversal
• The nodes of tree would be visited in the order: 4 2 5 1 6 3 7
Postorder Traversal
• In postorder traversal, the root is visited after both subtrees. Postorder traversal is
defined as follows:
• Traverse the left subtree in Postorder.
• Traverse the right subtree in Postorder.
• Visit the root.
void postorder (Node *p)
{ if(p!= Null)
{ postorder (p left);
postorder (p right);
printf(“%d” , p data);
}
}
Postorder Traversal
• The nodes of the tree would be visited in the order: 4 5 2 6 7 3 1
Construction of binary tree from inorder and
postorder.
1. Choose each node from right to left from postorder traversal.
2. For its placement, refer inorder traversal.
Inorder – 8, 9, 10, 15, 20, 25
Postorder- 9, 8, 15, 25, 20, 10
Algorithm to find the maximum element in a tree
int FindMax(struct Node *root) {
int root_val, left, right, max= INT_MIN;
if(root != NULL) {
root_val = root data;
left = FindMax(root left);
right = FindMax(root right);
if (left> right)
max=left;
else
max= right;
if(root_val > max)
max= root_val;
}
return max;
}
Binary Search Trees
• In binary search trees, all the left subtree elements should be less than
root data and all the right subtree elements should be greater than root
data.
• This property should be satisfied at every node in the tree.
• The left subtree of a node contains only nodes with keys less than the
nodes key.
• The right subtree of a node contains only nodes with keys greater than
the nodes key.
• Both the left and right subtrees must also be binary search trees.
• Inorder traversal of BST displays the value in ascending order.
• No duplicates allowed in a BST.
Which of these is a BST?
Algorithm to find an element in a BST
struct BSTNode *Find (struct BSTNode *root, int data) {
if (root == NULL)
return NULL;
while(root) {
if (data ==root data)
return root;
else if (data > root data)
root = root right;
else
root = root left;
}
return NULL;
Finding Maximum element in BST
struct BSTNode *FindMax(struct BSTNode *root) {
if(root == NULL)
return NULL;
else if(root right == NULL)
return root;
else
return FindMax(root right);
}
Insertion in BST
struct node
{
int data;
struct node *left;
struct node *right;
};
Insertion in BST
Step 1: IF TREE = NULL
Allocate memory for TREE
SET TREE -> DATA = ITEM
SET TREE -> LEFT = TREE -> RIGHT = NULL
ELSE
IF ITEM < TREE -> DATA
Insert(TREE -> LEFT, ITEM)
ELSE
Insert(TREE -> RIGHT, ITEM)
[END OF IF]
[END OF IF]
Step 2: END
Insertion in BST
Input: [40,20,60,10,30,50,70,25]
40
20 60
10 30 50 70
What about 25?
Deletion in BST
To delete a node from BST, three possible situations occur -
• The node to be deleted is the leaf node, or,
• The node to be deleted has only one child, and,
• The node to be deleted has two children
Deletion in BST
Case I: If the node is leaf (both left and right will be NULL),
remove the node directly and free its memory.
Deletion in BST
Case II: If the node has only one child, replace the child node
with the node to be deleted and delete the (leaf) node.
Deletion in BST
Case II: If the node has only one child, replace the child node
with the node to be deleted and delete the (leaf) node.
Deletion in BST
Case III: If the node to be deleted has two children.
• First, find the inorder successor of the node to be deleted.
• After that, replace that node with the inorder successor or predecessor
until the target node is placed at the leaf of tree.
• And at last, replace the node with NULL and free up the allocated
space.
Deletion in BST
Deletion in BST
Deletion in BST
• Delete 8
9
Extended Binary Trees
• An extended binary tree is also known as a 2-tree.
• A binary tree can be converted to an extended binary tree by adding
new nodes to its leaf nodes and to the nodes that have only one child.
• The new nodes that are added to binary tree (to make it extended
binary tree) are called external nodes.
Extended Binary Trees
General Trees
• General tree is a tree in which each node can have either zero or many
child nodes.
• It can not be empty.
• In general tree, there is no limitation on the degree of a node.
• The topmost node of a general tree is called the root node. There are
many subtrees in a general tree.
• In a general tree, each node has in-degree(number of parent nodes)
one and maximum out-degree(number of child nodes) n.
AVL (Adelson-Velskii and Landis) Trees
• AVL tree is a binary search tree with a balance condition: the
difference between left subtree height and right subtree height is at
most 1.
• For every node, |HLT – HRT| <= 1
Which of these is an AVL Tree?
AVL Tree Declaration
struct AVLTreeNode{
struct AVLTreeNode *left;
int data;
struct AVLTreeNode *right;
int height;
};
Rotations
• RR single (anticlockwise)
• Insertion into right subtree of right child of α makes the tree unbalanced.
• LL single (clockwise)
• Insertion into left subtree of left child of α makes the tree unbalanced.
• LR double (RR+LL)
• Insertion into right subtree of left child of α makes the tree unbalanced.
• RL double (LL+RR)
• Insertion into left subtree of right child of α makes the tree unbalanced.
Left Rotation (LL)
Right Rotation (RR)
LR Rotation
RL Rotation
AVL Trees Visualization