Have Simple c Code to create Binary tree

Now the code written bellow will let u start creating binary treee data structure.
Firstly know wht is it exactly before start coding
binary tree data structure is arrengement of element in a manner in that first node would be the root of tree, then next coming node would be added as the rule bellow
1 if the next node element is smaller then the root the it would be added as left child of root
otherwise right one. and remeber intis data structure each node can have only two childs.
after first iterartin we will find the suitable node to add the next and the add(search node-add -node)

start coding from here
structure of node would be
typedef struct TreeNode
{
int data; //data field
struct TreeNode *LeftNode;//left child niode address
struct TreeNode *RightNode;//right child niode address
}treenode;

//Making a global root
treenode *root =NULL;
now function to create node...
treenode *makenode(int data)
{
treenode * tempnode = (treenode *)malloc(sizeof(treenode));
tempnode->Data = data;
tempnode->LeftNode =NULL;
temp->RightNode = NULL;
return tempnode;
}
//noe time to create the tree
void create_btree(treenode *root,treenode*child)
{
treenode *tempnode =root;
if(root == NULL)//check the first node in tree become root of node
{
root = child;

}
else
{
// now will search for the suitable place according rulr
tempnode = root;
while(tempnode)
{
if(tempnode->Data > child->Data)
{
if(tempnode->left == NULL)
tempnode->LeftNode = Child;
else
tempnode= tempnode->LeftNode;
}
else
{
if(temp->RightNode == NULL)
tempnode->RightNode = child;
else
tempnode = tempnode->RightNode;
}
}}}
put the above function in main in loop to crate runtime tree
sample of main
void main()
{
create_btree(root,makenode(2));
create_btree(root,makenode(1));
create_btree(root,makenode(3));
}
//enjoy coding
to see the tree data there is three type of traversal
1-inorder
2-preorder
3-post order
get code in next post................
enjoy

Comments

Popular posts from this blog

XML Programming using IXMLDOMDOCUMENT in vc++

Get correct OS version on win 8.1 without using GetVesionEx API (deprecated )