Write an algorithm and program that creates a Binary Search Tree with a set of given inputs. Then, it should prompt for a key value and print a message about the presence of key value in the Binary Search Tree.
Answers
Answer:
First we need to create the BST
#include<stdio.h>
struct Treenode
{ int child;
struct Treenode *l, *r;
};
// below is the process to make new node in BST
struct Treenode * insert(struct Treenode * Treenode , int child)
{
if (Treenode == NULL) return newTreenode(child);
if (child< Treenode ->child)
Treenode ->l= insert(Treenode ->l, child);
else if (child> Treenode ->child)
Treenode ->r= insert(Treenode ->r, child);
return Treenode ;
}
First part of the problem is done
Now for the second part here we are using inorder traversal to find a particular key
void inorder(struct Treenode *base)
{
if (base!= NULL)
{
inorder(base->l);
printf("%d the particulr key\n", base->child);
inorder(base->r);
}
}
int main()
{
struct node *base= NULL;
base= insert(base, 80);
insert(base, 10);
insert(base, 20);
insert(base, 30);
insert(base, 40);
insert(base, 50);
insert(base, 60);
inorder(base);
return 0;
}