Skip to main content

Binary Search Tree - BST implementation


Binary Search Tree - BST implementation

#include<stdio.h>
#include<stdlib.h>
typedef struct tree
{
int number;
struct tree *leftChild;
struct tree *rightChild;
} node;
node *root=NULL;
void insertNode(int value);
void searchOnTree(int value);
void preOrderPrint(node *rootNode);
void inOrderPrint(node *rootNode);
void postOrderPrint(node *rootNode);
int main()
{
insertNode(45);
insertNode(54);
insertNode(40);
insertNode(49);
insertNode(38);
insertNode(70);
insertNode(30);
insertNode(39);
insertNode(41);
insertNode(45);
insertNode(44);
printf("\nPre-Order Tree printing:\n");
preOrderPrint(root);
puts("");
printf("\nIn-Order Tree printing:\n");
inOrderPrint(root);
puts("");
printf("\nPost-Order Tree printing:\n");
postOrderPrint(root);
puts("");
searchOnTree(70);
return 0;
}
void insertNode(int value)
{
node *tempNode;
node *currentNode;
node *parentNode;
tempNode = (node *) malloc(sizeof(node));
tempNode->number = value;
tempNode->leftChild = NULL;
tempNode->rightChild = NULL;
//For the very first call
if(root==NULL)
{
root = tempNode;
}
else
{
currentNode = root;
parentNode = NULL;
while(1)
{
parentNode = currentNode;
if(value <= parentNode->number)
{
currentNode = currentNode->leftChild;
if(currentNode==NULL)
{
parentNode->leftChild = tempNode;
return;
}
}
else
{
currentNode = currentNode->rightChild;
if(currentNode==NULL)
{
parentNode->rightChild = tempNode;
return;
}
}
}
}
}
void searchOnTree(int value)
{
node *currentNode = root;
int flag = 0;
while(1)
{
if(value == currentNode->number)
{
flag = 1;
break;
}
else if(value<=currentNode->number)
currentNode = currentNode->leftChild;
else
currentNode = currentNode->rightChild;
if(currentNode==NULL)
break;
}
if(flag==1)
printf("\n%d is found on Tree.\n\n", currentNode->number);
else
printf("\n%d is not found on Tree.\n\n", value);
}
void preOrderPrint(node *rootNode)
{
if(rootNode==NULL)
return;
printf("%d ", rootNode->number);
preOrderPrint(rootNode->leftChild);
preOrderPrint(rootNode->rightChild);
}
void inOrderPrint(node *rootNode)
{
if(rootNode==NULL)
return;
inOrderPrint(rootNode->leftChild);
printf("%d ", rootNode->number);
inOrderPrint(rootNode->rightChild);
}
void postOrderPrint(node *rootNode)
{
if(rootNode==NULL)
return;
postOrderPrint(rootNode->leftChild);
postOrderPrint(rootNode->rightChild);
printf("%d ", rootNode->number);
}

Comments

Popular posts from this blog

**Competitive Programming এর জন্য কি কি শিখতে হবে...**

link: https://github.com/me-shaon/bangla-programming-resources -------------------------------------------------------------------------------------------------------------------------- এলগোরিদম ব্যাসিক বিগ "O" নোটেশন  -  শাফায়েত আশরাফ কমপ্লেক্সিটি ক্লাস(P-NP, টুরিং মেশিন ইত্যাদি)  -  শাফায়েত আশরাফ ডাটা স্ট্রাকচার অ্যাারে (Array) অ্যারে ব্যাসিক অপারেশন  -  হাসান আবদুল্লাহ অ্যারে কমপ্রেশন/ম্যাপিং  -  শাফায়েত আশরাফ লিংকড লিস্ট (Linked List) লিংকড লিস্ট  -  শাফায়েত আশরাফ লিংকড লিস্ট ব্যাসিক অপারেশন  -  হাসান আবদুল্লাহ লিংকড লিস্ট  -  অনিন্দ্য পাল লিংকড লিস্ট – সি  -  মুনতাসির ওয়াহেদ ডাটা স্ট্রাকচার ও লিংকড লিস্ট  -  আলাভোলা কোডিং লিংকড লিস্ট  -  আলাভোলা ডাবলি লিংকড লিস্ট  -  মুনতাসির ওয়াহেদ স্ট্যাক (Stack) স্ট্যাক  -  শাফায়েত আশরাফ স্ট্যাক ব্যাসিক অপারেশন  -  হাসান আবদুল্লাহ স্ট্যাক বেসিক ডাটা স্ট্রাকচার  -  আহম...

codeforce problemset 758A solved

A. Holiday Of Equality time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are  n  citizens, the welfare of each of them is estimated as the integer in  a i  burles (burle is the currency in Berland). You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. Input The first line contains the integer  n  ( 1 ≤  n  ≤ 100 ) — the number of citizens in the kingdom. The second line contains  n  integers  a 1 ,  a 2 , ...,  a n , where  a i  ( 0 ≤  a i  ≤ 10 6 ) — the welfare of the  i -th citizen. Output In the only li...

700 problems to understand you complete algorithmic programming.

700 problems to understand you complete algorithmic programming. 1. Segment Tree: To Read : http://www.topcoder.com/tc?d1=tutorials&d2=lowestCommonAncestor&module=Static http://ronzii.wordpress.com/2011/07/08/segment-tree-tutorial/ http://se7so.blogspot.in/2012/12/segment-trees-and-lazy-propagation.html http://olympiad.cs.uct.ac.za/presentations/camp3_2007/interval_trees.pdf http://codeforces.com/blog/entry/6281 http://apps.topcoder.com/forums/?module=Thread&threadID=651820&start=0&mc=2#1146133 http://www.algorithmist.com/index.php/Segmented_Trees http://letuskode.blogspot.in/2013/01/segtrees.html http://wcipeg.com/wiki/Heavy-light_decomposition http://discuss.codechef.com/questions/5960/rnestescape-from-the-mines http://ideone.com/dPS5N  (Heavy Light implementation). https://sites.google.com/site/indy256/algo/heavy_light  (Heavy Light implementation). Problems: http://www.spoj.com/problems/GSS1 http://www.spoj.com/problems/GSS2 http://www.spoj.com...