Basics of Binary Tree and Binary Search Tree
Basics of Binary Tree and Binary Search Tree
Prof.B.B.Kotame
Binary Tree
• In binary tree, every node can have at A
• Definition:
1. For every node may have left sub tree and right sub tree whereas in tree
sub tree doesn’t matter.
2. Binary tree can have zero nodes i.e. binary tree can be empty, which is
not in case of tree
3.
A A In this example 1st binary tree has empty right sub tree
while second binary tree has empty left tree. if we consider
B
B
as tree then both are same only representation is different.
= log2 (15+1)=log(16)/log(2)
=4 (n<= 2h-1)
2. In a binary tree, the number of internal nodes of degree-1 is 5 and the number of internal nodes of degree-2
is 10. The number of leaf nodes in the binary tree is ______?
3. A binary tree T has 20 leaves. The number of nodes in T having 2 children is ______?
B B
node is having either only left sub tree or right sub
C C
tree
A
2. Almost Complete Binary Tree: In a complete
binary tree, each non-leaf node compulsory has sub B C
tree. Also, in the last or the lowest level of this
binary tree, every node should possibly reside on the D E F G
- In other words internal node will have either two children or no child at all.
B C
D E
F G
- Each empty sub tree is replaced by a failure node. A failure node is represented by
- Any binary tree can be converted into a extended binary tree by replacing each
empty sub tree by a failure nodes
D E F G D G
7 A B C D E F G 13 A B C - G D - - - - - E F
• Therefore another way is needed to represent the Binary tree. That is nothing but
linked representation which is an efficient way than array.
• The code to write a tree node would be similar to what is given below. It has a data part and references
to its left and right child nodes.
A
A
B
B C H
K
D F I J
E
D D
B C B C
F H
E G I J E
H
F G K
J
I
The basic operations that can be performed on a binary tree data structure, are the
following −
Insert − Inserts an element in a tree/create a tree.
Traversals − Searches an element in a tree.
Preorder Traversal − Traverses a tree in a pre-order manner.
Inorder Traversal − Traverses a tree in an in-order manner.
Postorder Traversal − Traverses a tree in a post-order manner.
Search- Search an element in tree using traversals.
Delete- delete an element from binary tree
Create/Insert OPERATION
• The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first locate its
proper location. Start searching from the T node, then search for the empty location in the left subtree
and insert the data. Otherwise, search for the empty location in the right subtree and insert the data.
• Algorithm:
1. Enter key to be inserted. Create node as tempNode for it.
2. Check T is NULL, if yes then make tempNode as T of tree
3. If T is not NULL then
4. Take temporary variable *ptr and set *ptr=T
5. Do
i. Ask user in which direction user wants to insert data(Left or right)
ii. if direction is Left the check following condition
iii. if(ptr->left==NULL) //insert node at left of tree
else
{ {
ptr->left=tempNode; ptr=ptr->right;
}
break; } }
else{ iv. Do step 5 while(ptr!=NULL)
ptr=ptr->left; 5. Repeat steps 1 to 5 until no more data to
insert.
} 6. Stop
else //if user gives right direction
{
if(ptr->right==NULL)
{
ptr->right=tempNode;
break;
}
• Btree.cpp
• Enter the element=4
• Do u want to enter more elements (y/n)y
• Enter the element=12
• in which direction(l/r)l
• Do u want to enter more elements (y/n)y 4
• Enter the element=34
• in which direction(l/r)l 12 1
• in which direction(l/r)?r
• Do u want to enter more elements (y/n)y
22
• Enter the element=1
34 32
• in which direction(l/r)r
• Do u want to enter more elements (y/n)y
• Enter the element=22
• in which direction(l/r)r
• in which direction(l/r)?r
• Do u want to enter more elements (y/n)y
• Enter the element=32
• in which direction(l/r)r
• in which direction(l/r)?l
Binary Tree Traversal
• Traversal is a process to visit all the nodes of a tree and print their values too. Because, all nodes are
connected via edges (links) we always start from the T (head) node.
• We cannot access a node randomly from a tree. There are three ways which we use to traverse a tree −
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
1. In - Order Traversal ( LeftChild - T - RightChild )
Inorder Traversal : B E D F A G C H
2. Pre - Order Traversal ( T - LeftChild - RightChild )
Step 1 : Start from the Left Subtree (Last Leaf) and visit it.
Step 2 : Then, go to the Right Subtree.
Step 3 : Then, go to the T.
A Stack
Operations
push(root)
B C
temp=pop() A B C D
temp=
D push(temp->right)
push(temp->left)
while(stack is not empty) A
{
temp=pop();
print temp->data;
if temp has right child then push into stack
if temp has left child then push into stack
}
A
B C
D F
pop temp
display temp->data
move temp to its right
} while(!isempty())
4. Stop
A
B C
In this traversal each node is visited twice but we are supposed to dispaly
only once.
- Hence there should be some mechanism to indicate whether the node is being
visited 1st time or 2nd time.
- Display the content of the node only when it is visited for the 2nd time.
• Algorithm:
1. S is an empty stack used to store Node
pointer
2. Node *temp= root
3. do
{ A
while(temp!=NULL)
s.push(temp)
temp=temp->left B C
B C
• inorder,preorder and postorder requires stack for traversal, but there are some
traversal techniques which requires queue e.g. BFS
• This traversal is also called as Level order traversal as it visits nodes in levels
e.g.
10
10 5 20 1 7 15
5 20
1 7 15
• Algorithm
10
1. temp=root
2. q.enqueue(temp)
3. while(!isemptyq()) 5 20
{
temp=dqueue(); 1 7 15
print temp->data;
if(temp->left!=NULL)
q.enqueue(temp->left)
if(temp->right!=NULL)
q.enqueue(temp->right)
}
4. Stop
• BFS is generally used for finding minimum cost edges.
10
5 20 10 5 1 7 20 15
1 7 15
• Algorithm
A
10 visit A and push(A) A
pop and display -1 A
push D BD A
5B D push B
pop B D AB
display B
C E F
push C pop C and D ABC
display
pop D E F A BC D
display D &push E,F
pop E,display E -1 ABCDEF
pop F, display F
Binary Search Tree
• When we store order data into array structure we use efficient search algorithm.e.g. Binary Search
• However, to provide efficient insertion and deletion operation,we represent a link list.
• The problem with linked list is that their search algo. which are sequential searches,very
ineffecient.
• So what we need is data structure that has efficient search algo. and at the same time efficient
insertion and deletion operation.
Defination: A Binary search tree is a binary tree with following properties: Key
58 20
6 19 6 58 20 22 19
11 15 12
19 5 9 25
Binary Search Tree(BST)
• A binary search tree (BST) is a binary tree that is either empty or in
which every node contains a key (value) and satisfies the following
conditions:
• All keys in the left sub-tree of the root are smaller than the key in the
root
node
• All keys in the right sub-tree of the root are greater than the key in the root
node
• The left and right sub-trees of the root are again binary search trees
Ashim Lamichhane
Binary Search Tree(BST)
• A binary search tree is basically a binary tree, and therefore it can
be traversed in inorder, preorder and postorder.
Ashim Lamichhane
Binary Search Tree(BST)
Time Complexity
Array Linked List BST
Search O(n) O(n) O(logn)
Insert O(1) O(1) O(logn)
Remove O(n) O(n) O(logn)
Ashim Lamichhane
A
B E
Correct A
tree D C F
B E
I G
H
D G F
A
I C
H
B E
C G F
D I
H