Professional Documents
Culture Documents
Anirudh Agnihotry
INTRODUCTION
Arrays , stacks , linked lists , queues are linear data structures whereas trees
are hierarchical data structures.
Trees are of various types e.g. binary trees ,Avl trees , Binary search trees.
A tree whose elements have at most two children is called a binary tree . As it
can have only two children we call them as left and right child
Structure of Node
A node in a tree consists of following parts
Data
Pointer to right child
Pointer to left child
Parent pointer (optional)
Continued ...
5
root
Internal
node
leaf
struct node
{
int data;
struct node *left;
struct node *right;
};
Building a Tree
int main()
{
node* root=create_node(5);
To traverse (or walk) the binary tree is to visit each node in the binary tree
exactly once
There are three kinds of traversals
Preorder :- node,left,right
Inorder :- left,node,right
Postorder:- left,right,root
Inorder walk
Inorder walk(root)
1.
2.
3.
Inorder walk(root->left)
Visit node
Inorder walk(root->right)
Time Complexity ?
Time complexity ?
Height of a Tree
Height of a node is the length of the longest path in its subtree to a leaf
Height of a Binary Tree is the height of its root node.
Given a Binary Tree, write a function which calculates its height.
Time complexity ?
Height of a leaf ?
Maximum height possible with N nodes ?
Questions ?
The left subtree of a nodes contains nodes with keys with value less than its own key
The right subtree of a nodes contains nodes with keys with value higher than its own key
The left subtree and right subtree should be binary search trees
The above ordering among keys make search operation faster but slows
down insertion.
Examples
2
10
11
6
2
15
Time complexity ?
Space Complexity ?
Complexity
Time : O(h) where h is the height of the tree.
In the worst case, h may be O(n)!
Space : Need O(h) for stack space for the recursive algorithm. It can be done
iteratively in constant space.
Insert In a BST
Algorithm(key)
Insert 12
12 > 10
13
13>12
12
15
10
Int main()
{
node* root = NULL;
root = Insert(root,10);
Insert(root,20);
Insert(root,30);
}
20
30
Deletion in BST
delete(root,key)
1.
2.
3.
c.
d.
Case 1 :- The node has no children then replace the link in parent node with NULL
Case 2:- the node has both left and right child
i. Find the leftmost element in right subtree and leftmost element in right subtree,swap
with the node.
ii. Now delete the leftmost element
Case 3:- The node has no left child
i. Link the parent of the node to right subtree
Similarly when the node has no right child
parent
10
12
Removing 12
replace the link in the parent with
null
10
Removing 10
replace the link in the parent with its
non-null child
Removing 4
Swap 4 with the leftmost node and
remove not that node.
Final tree
Properties of BST
The balanced binary trees are those trees in which the height difference of left
child node and right child node is atmost 1.
Height of such trees become atmost O(logn).
So all the insert,search and delete operations can be done in O(logn).
Thanks :)