Binary Tree
Binary Tree
Binary Tree
A Binary tree is a hierarchical data structure in which every node has 2
children, also known as left child and right child, as each node has 2
children hence the name "Binary".
L=I+1
where L = Number of leaf nodes, I = Number of internal nodes.
A Perfect Binary Tree of height h (where the height of the binary tree is the
longest path from the root node to any leaf node in the tree) has 2h+1 – 1
node.
Visual Representation:
Binary Tree representation
As you can see, the topmost node is the
2. Linked-list Representation parent node of nodes "B" and "C". Each
node contains a pointer to the left subtree
and pointer to the right subtree. Node A
has a pointer to the left child that is B and
a pointer pointing to the right child of A
that is B. Middle box represents the data in
the node. The nodes which doesn't have a
child are called as leaf nodes. These nodes
do not have pointer to their subtrees.
Therefore left and right pointers are set to
NULL.
• left child node’s value is always less than the parent Node
• right child node has a greater value than the parent node.
• all the nodes individually form a binary search tree.
Binary Search Tree properties
The left sub tree of a node only contain nodes less than the parent node’s key.
The right sub tree of a node only contains nodes greater than the parent node's
key.
Example of a binary search tree (BST)
A binary search tree is created in order to reduce the complexity of operations like
search, find minimum and maximum.
1. Search operation in BST
If the value of the key is greater than the root node, take the right subtree
and search for the key.
If the value of the key is less than the root node, take left subtree and
search for the key.
Example of Search Operations
#include <iostream>
using namespace std;
struct node{
int key;
struct node *left, *right;
};
if(search(root , 12))
cout<<"\n Element found";
else
cout<<"\n Element not found";
return 0;
}
Output
2. Insertion operation in BST
Insertion operation in a BST takes place at the leaf node of the tree for
insertion we will start the comparison of the node with the root node and
find the correct position of the node and then place it. The following
example will make it more clear to you.
8
Inserting 12 to this BST.
We will compare 12 with root node 12 > 5, it belongs to the right subtree.
Compare 12 with the right child node 12 > 8, it belongs to the right of the
right sub child.
Compare 12 with the right sub child of right subtree 12 >10, its position is
the right of this node.
The new tree formed will be,
8
Example
#include <iostream>
using namespace std;
struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Example
//Cont
void traversetree(struct node *root){
if (root != NULL){
traversetree(root->left);
cout<<"\t" <<root->key;
traversetree(root->right);
}
}
struct node* insert(struct node* node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
Example
//Cont
int main(){
struct node *root = NULL;
root = insert(root, 23);
insert(root, 15);
insert(root, 12);
insert(root, 17);
insert(root, 32);
insert(root, 29);
Deleting a leaf node from the tree: The simplest deletion is the deletion
of a leaf node from the binary search tree. For deleting the leaf node
only the leaf gets affected. Example,
8
deleting the leaf node 7 gives,
8
Deleting the node with one child node: for this deletion, you need to replace
the child node with the node to be deleted and then delete it. Example,
8
Deleting 2 from the BST
8
•Deleting the node with two child nodes: Here the node to be deleted
has two child nodes. So, we will use in the order form of the tree, here
we will delete the element and select its in order neighbor for its place
and recreate the rest. Example,
8
Deleting 5 from the BST will return the following tree.
Example
#include<iostream>
using namespace std;
struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Example
//Cont
void inordertraversal(struct node *root){
if (root != NULL){
inordertraversal(root->left);
cout<<root->key;
inordertraversal(root->right);
}
}
struct node* insert(struct node* node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
Example
//Cont
struct node * minValueNode(struct node* node){
struct node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, int key){
if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
Example
//Cont
else{
if (root->left == NULL){
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL){
struct node *temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
Example
//Cont
return root;
}
int main(){
struct node *root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
cout<<"\nDelete 30\n";
root = deleteNode(root, 30);