Binary Tree
Binary Tree
Binary tree is a special tree data structure in which each node can have at most 2 children.
Thus, in a binary tree,
Each node has either 0 child or 1 child or 2 children.
Example-
A binary tree is unlabeled if its nodes are not assigned any label.
Example-
Consider we want to draw all the binary trees possible with 3 unlabeled nodes.
Using the above formula, we have-
Thus,
• With 3 unlabeled nodes, 5 unlabeled binary trees are possible.
• These unlabeled binary trees are as follows-
Consider we want to draw all the binary trees possible with 3 labeled nodes.
Using the above formula, we have-
Thus,
• With 3 labeled nodes, 30 labeled binary trees are possible.
• Each unlabeled structure gives rise to 3! = 6 different labeled structures.
Similarly,
• Every other unlabeled structure gives rise to 6 different labeled structures.
• Thus, in total 30 different labeled binary trees are possible.
A rooted binary tree is a binary tree that satisfies the following 2 properties-
• It has a root node.
• Each node has at most 2 children.
Example-
2. Full / Strictly Binary Tree-
• A binary tree in which every node has either 0 or 2 children is called as a Full binary
tree.
• Full binary tree is also called as Strictly binary tree.
Example-
Here,
• First binary tree is not a full binary tree.
• This is because node C has only 1 child.
A complete binary tree is a binary tree that satisfies the following 2 properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Example-
Here,
• First binary tree is not a complete binary tree.
• This is because all the leaf nodes are not at the same level.
An almost complete binary tree is a binary tree that satisfies the following 2 properties-
• All the levels are completely filled except possibly the last level.
• The last level must be strictly filled from left to right.
Example-
Here,
• First binary tree is not an almost complete binary tree.
• This is because the last level is not filled from left to right.
A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Example-
Binary Search Tree Operations-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
1. Search Operation-
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-
Example-
2. Insertion Operation-
Rules-
The insertion of a new key always takes place as the child of some leaf node.
For finding out the suitable leaf node,
• Search the key to be inserted from the root node till some leaf node is reached.
• Once a leaf node is reached, insert the key as child of that leaf node.
Example-
Consider the following example where key = 40 is inserted in the given BST-
3. Deletion Operation-
Deletion Operation is performed to delete a particular element from the Binary Search Tree.
When it comes to deleting a node from the binary search tree, following three cases are
possible-
Just remove / disconnect the leaf node that is to deleted from the tree.
Example-
Consider the following example where node with value = 20 is deleted from the BST-
Case-02: Deletion Of A Node Having Only One Child-
Just make the child of the deleting node, the child of its grandparent.
Example-
Consider the following example where node with value = 30 is deleted from the BST-
A node with two children may be deleted from the BST in the following two ways-
Method-01:
Consider the following example where node with value = 15 is deleted from the BST-
Method-02:
Example-
Consider the following example where node with value = 15 is deleted from the BST-
Time Complexity-
BST Traversal-
• A binary search tree is traversed in exactly the same way a binary tree is traversed.
• In other words, BST traversal is same as binary tree traversal.
Example-
Now, let us write the traversal sequences for this binary search tree-
Preorder Traversal-
Inorder Traversal-
Postorder Traversal-
Important Notes-
Note-01:
• Inorder traversal of a binary search tree always yields all the nodes in increasing
order.
Note-02:
Problem-01:
Solution-
Method-01:
Method-02:
We know, inorder traversal of a binary search tree always yields all the nodes in
increasing order.
Inorder Traversal :
0,1,2,3,4,5,6,7,8,9
Solution-
In this question,
• We are provided with the preorder traversal sequence.
• We write the inorder traversal sequence by arranging all the numbers in ascending
order.
Then-
• Postorder Traversal : 30 , 20 , 10 , 15 , 25 , 23 , 39 , 35 , 42
• Inorder Traversal : 10 , 15 , 20 , 23 , 25 , 30 , 35 , 39 , 42
Now,
• We draw a binary search tree using these traversal results.
• The binary search tree so obtained is as shown-
Now, we write the postorder traversal sequence-
Postorder Traversal :
15 , 10 , 23 , 25, 20, 35, 42, 39, 30