Presentation On Binary Search Tree using Linked List Concept which includes Traversing the tree in Inorder, Preorder and Postorder Methods and also searching the element in the Tree
The Ultimate Guide to Choosing WordPress Pros and Cons
Binary Search Tree
1. Binary Search Tree
Aakash Montheiro - 161702
Abhishek.L.R - 161704
Deelan Jostan Monthero - 161726
I Sem - M.C.A
Aloysius Institute Of Management &
Information Technology
2. What is …?
• A Binary tree is a non-linear data structure which is a collection of elements
called nodes.
• A Binary Search Tree (BST) is a Binary tree in which all the nodes follow the
below-mentioned properties :
The left sub-tree of a node has a key less than or equal to its parent node's key.
The right sub-tree of a node has a key greater than to its parent node's key.
• Thus, BST divides all its sub-trees into two segments;
Left sub-tree
Right sub-tree
3. Operations Performed in a BST:
Search − Searches an element in a tree.
Insert − Inserts an element in a tree.
Traversal :
Pre-order Traversal − Traverses a tree in a pre-order manner. [Root->Left->Right]
In-order Traversal − Traverses a tree in an in-order manner. [Left->Root->Right]
Post-order Traversal − Traverses a tree in a post-order manner.[Left->Right->Root]
6. Implementation:
• Creating the Structure
typedef struct Tree{
int Data;
struct Tree *Right;
struct Tree * Left;
}TREENODE;
Data *Right*Left
Null Null
12. int BTree::TreeSearch(int Num)
{
TREENODE *Cur;
Cur=Root;
while(Cur){
if(Num==Cur->Data){
return 1;
}else if(Num>Cur->Data){
Cur=Cur->Right;
}else{
Cur=Cur->Left;
}
}
}
Implementation Contd…
Searching the Element
13. Implementation Contd…
Inserting elements in Main Program cout<<"Enter the value of N : ";
cin>>N;
cout<<"Enter the elements n";
for(i=1;i<=N;i++){
cout<<"Enter the element A["<<i<<"] : ";
cin>>Num;
BT.InsertTreeNode(Num);
}
Declaration in Main Program
TREENODE *Root;
BTree BT;
14. Implementation Contd…
Root=BT.GetRoot();
cout<<"Pre-order traverse is : ";
BT.Preorder(Root);
cout<<"n";
break;
Traversing the TreeSearching of Elements
cout<<"Enter the Search element : ";
cin>>Search;
Res=BT.TreeSearch(Search);
if(Res==1){
cout<<"Element "<<Search<<" Found...n";
}else{
cout<<"Element "<<Search<<" Not-found....n";
}
break;
Click here to download Source Code