Dsa Mod4 - Part2
Dsa Mod4 - Part2
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes with
smaller values and right subtree of every node contains larger values.
Every Binary Search Tree is a binary tree but all the Binary Trees need not to be binary search trees.
Example
Construct a Binary Search Tree by inserting the following sequence of numbers...
10, 12, 5, 4, 20, 8, 7, 15 and 13
Search
Insertion
Deletion
Traversal
root->right=insert(root->right,key);
return root;
}
Deletion Operation in BST
Deleting a node from Binary search tree has following three cases...
Case 1: Deleting a Leaf node (A node with no
children)Case 2: Deleting a node with one child
Case 3: Deleting a node with two children
tree.
node* deletekey(node *root,int key)
{
node *temp;
if(root==NULL)
{
printf("Key not found");
return NULL;
}
if(key<root->data)
root->left=deletekey(root->left,key);
else if(key>root->data)
root->right=deletekey(root->right,key);
else
{
if(root->left==NULL && root->right==NULL)
{
free(root);
return NULL;
}
else if(root->left==NULL)
{
temp=root;
root=root->right;
free(temp);
return NULL;
}
else if(root->right==NULL)
{
temp=root;
root=root->left;
free(temp);
return NULL;
}
else
{
temp=findleft(root->right);
root->data=temp->data;
root->right=deletekey(root->right,temp->data);
}
}
return root;
}
}
Counting the number of leaf nodes in BST
int leafnodes(node *root)
{
static int count=0;
if(root!=NULL)
{
leafnodes(root->left);
if(root->left==NULL && root->right==NULL)
count++;
leafnodes(root->right);
}
return count;
}
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
Preorder Traversal Function
If root is not NULL, Print root->data.
Recursively call preorder on the left child.
Recursively call preorder on the right child.
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}
#include<stdio.h>
#include<stdlib.h>
struct BST
{
int data;
struct BST *left;
struct BST *right;
};
typedef struct BST node;
node* insert(node *root,int key)
{
if(root==NULL)
{
root=(node*)malloc(sizeof(node));
root->data=key;
root->left=NULL;
root->right=NULL;
return root;
}
if(key<root->data)
root->left=insert(root->left,key);
else if(key>root->data)
root->right=insert(root->right,key);
return root;
}
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%d\t",root->data);
inorder(root->right);
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%d\t",root->data);
preorder(root->left);
preorder(root->right);
}
}
switch(choice)
{
case 1: printf("\ninorder is \n");
inorder(root);
break;
case 2: printf("\npre order is \n");
preorder(root);
break;
case 3: printf("\npost order is \n");
postorder(root);
break;
case 4:printf("\nenter the element to be searched");
scanf("%d",&key);
search(root,key);
break;
case 5: exit(0);
default:printf("invalid choice\n");
}
}
}
4.7 Threaded Binary Tree
Two many null pointers are there in current representation of binary trees
n: number of nodes
number of non-null links (edges): n-1
total links: 2n
null links: 2n-(n-1)=n+1
Replace these null pointers with some useful “threads”.
To construct the threads we use the following rules (assume that ptr represents a node) :
If ptr->left_child is null,
replace it with a pointer to the node that would be
visited before ptr in an inorder traversal
If ptr->right_child is null,
replace it with a pointer to the node that would be
visited after ptr in an inorder traversal
In the above figure two threads have been left dangling: one is the left child of H, the other is the right
child of G. In order that we leave no loose threads, we will assume a header node for all threaded binary
trees.
struct threadednode
{
int data;
struct threadednode *left;
struct threadednode *right;
int lthread;
int rthread;
};
Empty Threaded Binary Tree
Notice that we have handled the problem of the loose threads by having them point to the head node,
root.
Types of Threaded Binary Tree
There are two types of threaded Binary Tree:
In one-way threaded binary trees, a thread will appear either in the right or left link field of a node. If it
appears in the right link field of a node then it will point to the next node that will appear on performing
in order traversal. Such trees are called Right threaded binary trees. If thread appears in the left field of
a node then it will point to the nodes inorder predecessor. Such trees are called Left threaded binary
trees. Left threaded binary trees are used less often as they don't yield the last advantages of right
threaded binary trees. In one-way threaded binary trees, the right link field of last node and left link field
of first node contains a NULL. In order to distinguish threads from normal links they are represented by
dotted lines.
In two-way threaded Binary trees, the right link field of a node containing NULL values is replaced by a
thread that points to nodes inorder successor and left field of a node containing NULL values is replaced
by a thread that points to nodes inorder predecessor.