Binary Tree and BST
Binary Tree and BST
• Construct tree:
• The root of the tree is the node 60 at the top.
• Node 29 and 44 are the successors of the
node 60.
• The nodes 6, 4, 12 and 67 are the terminal
nodes.
Applications:
• Implementing the hierarchical structures in computer systems like
directories and file systems.
• Implementing the navigation structure of a website.
• Code generation like Huffman’s code.
• Decision-making in gaming applications.
• Implementation of priority queues for priority-based OS
scheduling functions
• Parsing of expressions and statements in programming language
compilers
• For storing data keys for DBMS for indexing
• Spanning trees for routing decisions in computer and communications
networks
• Hash trees
• Path-finding algorithm to implement in AI, robotics and video games
Properties of the Tree:
• The root of each sub-tree is a child of a Root node.
• Every node except the root node has one parent node and all the parent nodes have at least
one child.
• The node which has no child called leaf node or terminal nodes.
• The nodes with the same parent are called siblings.
Properties of the Tree:
• The depth of a node n is the unique path from root node to the node n.
• The height of a node n is the unique path from the node n to the root node.
• The height of the tree must be equal to the depth of the tree. Therefore H(T)= D(T).
• All the leaves at height zero and the depth of the root is zero.
• Level- A layer of parent nodes corresponding to a given a node of the tree.
• A tree should not have multiple paths from node n1 to node n2.
Binary Tree:
• Binary Tree is defined as a tree data structure where each node has at most 2 children. Since
each element in a binary tree can have only 2 children, we typically name them the left and
right child.
Types:
Full Binary Tree/ Proper Binary Tree:
• A full Binary tree is a special binary tree in which every parent node/internal node has two or no children.
Node:
struct node
{
int data;
struct node *left,*right;
}
Basic Operations:
1. Insertion
2. Traversal
3. Deletion • Other operations:
4. Searching • Find height of the tree
• Find number of nodes in the tree.
• Find the level of the tree
1. Insertion:
Steps to construct Binary Tree:
struct BTnode* createNode(value)
//Root node : level 0 node
{ struct BTnode* rootNode = createNode(7);
//Left node of root: level 1 node
struct BTnode* newNode = malloc(sizeof(struct BTnode));
newNode->data = value; insertLeftNode(rootNode, 4);
newNode->leftNode = NULL; //Right node of root
newNode->rightNode = NULL;
insertRightNode(rootNode, 8);
return newNode;
} //level 2 nodes
insertLeftNode(rootNode->leftNode, 1);
struct BTnode* insertLeftNode(struct BTnode* rootNode, int insertRightNode(rootNode->rightNode, 5);
value) insertLeftNode(rootNode->leftNode, 6);
insertRightNode(rootNode->rightNode, 3);
{
rootNode->leftNode = createNode(value);
return rootNode->leftNode;
}
deleteTree(struct BTnode*root){
if(root == NULL)
return;
/* Delete Left sub-tree */
deleteTree(root->left);
//deletion at root
else if(root->data==val) 7 1 2 3 1 2 3
{ 9 3 5 Viewer does not support full
SVG 1.1
9 3 5
//no child
if(root->left == NULL && root->right == NULL)
{
free(root);
}
Binary Search Tree:
Deletion
21 21
Operation:
else ifchild
//one (root->left == NULL)
{
BSTNode* temp = root;
Delete
root=root->right; 15 27 (15) 7 27
free(temp);
}
else if (root->right ==
NULL) 7 23 35 23 35
{ Viewer does not support full
SVG 1.1
BSTNode* temp = root;
root=root->left;
free(temp)
}
Binary Search Tree:
Deletion Operation:
//two children
else
{
BSTNode* temp = getMin(root->right);
root->data=temp->data;
root->right=deleteNode(root-
>right,temp->data); //removing
duplicate
2 2
} root;
return 1 3
}
Delete
1 2 (21) 1 2
5 7 5 7
7 1 2 3 7 1 3
9 3 5 Viewer does not support full 9 5
Polish notation Conversion– Binary tree
Given,
In-order - 4, 2, 5, 1, 3, 6
Pre-order- 1, 2, 4, 5, 3, 6
Post-order- ?
Polish notation Conversion– Binary search tree
In a binary search tree, in-order list will always be in a sorted or ascending order,
Given any one order, we can find the other one
Given,
In-order - ?
Pre-order- 5 3 1 2 4 6 8 7
Post-order- ?
Exercise
1. Given BT,
In-order – D, B, H, E, I, A, F, C, G
Pre-order- A, B, D, E, H, I, C, F, G
Post-order-
2. Given BST,
In-order - ?
Pre-order- ?