0% found this document useful (0 votes)
21 views55 pages

Binary Tree

The document discusses different types of binary trees including full, complete, perfect, and balanced binary trees. It describes the properties of each type of binary tree. It also covers binary tree representations using sequential and linked list representations. Binary search trees are described including their properties and examples of search, insertion, and deletion operations.

Uploaded by

Jeremy Cabudbud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
21 views55 pages

Binary Tree

The document discusses different types of binary trees including full, complete, perfect, and balanced binary trees. It describes the properties of each type of binary tree. It also covers binary tree representations using sequential and linked list representations. Binary search trees are described including their properties and examples of search, insertion, and deletion operations.

Uploaded by

Jeremy Cabudbud
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 55

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".

Root node is the topmost node of the tree.


Common Types of Binary Tree
1. Full Binary Tree
A binary Tree is a full binary tree if every node has 0 or 2 children. The
following are the examples of a full binary tree. We can also say a full binary
tree is a binary tree in which all nodes except leaf nodes have two children.
2. Complete Binary Tree
A Binary Tree is a Complete Binary Tree if all the levels are completely
filled except possibly the last level and the last level has all keys as left as
possible.
3. Perfect Binary Tree
A Binary tree is a Perfect Binary Tree in which all the internal nodes
have two children and all leaf nodes are at the same level.
In a Perfect Binary Tree, the number of leaf nodes is the number of internal
nodes plus 1

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.

An example of a Perfect binary tree is ancestors in the family. Keep a person


at root, parents as children, parents of parents as their children.
4. Balanced Binary Tree
A binary tree is balanced when no 2 siblings can have a height difference
of more than 1.
Binary Tree Representation
A tree is represented by a pointer to the topmost node in tree. If the
tree is empty, then value of root is NULL.

A Tree node contains following parts.


1. Data
2. Pointer to left child
3. Pointer to right child
Binary Tree representation
1. Sequential representation
In this representation, array structure is used
to implement the tree. Size of array is equal
to the total nodes in the tree, index of root
node is 0. If a node is at 'i' location then its
left child is at '2i' index and right child at '2i +
1' location in the array.

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.

To implement binary tree, we will define


the conditions for new data to enter into
our tree.
Binary search tree (BST)
It is a special type of tree which follows the following rules:

• 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

Performing a search in a binary search tree, we need to search for a key in


the tree. For this, we will compare the key with the root node of the tree.

If key equals to root node, the key is found.

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;
};

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* search(struct node* root, int key){
if (root == NULL || root->key == key)
return root;
if (root->key < key)
return search(root->right, key);
return search(root->left, key);
}
Example
//Cont
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;
}
int main(){
struct node *root = NULL;
root = insert(root, 23);
insert(root, 15);
insert(root, 12);
Example
//Cont
insert(root, 17);
insert(root, 32);
insert(root, 29);
insert(root, 45);

cout<<"The tree is :\n";


traversetree(root);
cout<<"\n Searching for 12 in this tree ";

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);

cout<<"The tree is : \n";


traversetree(root);
cout<<"\n Inserting 45 to the tree \n";
insert(root, 45);
cout<<"Tree after insertion is :\n";
Example
//Cont
traversetree(root);
return 0;
}
Output
3. Delete Operation binary search tree (BST)
delete operation is dropping the specified node from the tree. in case
deleting the nodes, there are three possibilities

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<<"Inorder traversal of the given tree \n";


inordertraversal(root);
Example
//Cont
cout<<"\nDelete 20\n";
root = deleteNode(root, 20);

cout<<"Inorder traversal of the modified tree \n";


inordertraversal(root);

cout<<"\nDelete 30\n";
root = deleteNode(root, 30);

cout<<"Inorder traversal of the modified tree \n";


inordertraversal(root);
Example
//Cont
cout<<"\nDelete 50\n";
root = deleteNode(root, 50);

cout<<"Inorder traversal of the modified tree \n";


inordertraversal(root);
return 0;
}
Output

You might also like