Write a program to create a binary Tree ?

Answer Posted / nikhil

//TRY THIS
# include<iostream.h>
# include<conio.h>

struct NODE
{
char Info;
NODE *Left_Child;
NODE *Right_Child;
};

class Create_BT
{
public:
public: NODE *Binary_Tree (char *, int, int);
void Output(NODE *, int );
};

// Function to insert an element into tree

NODE * Create_BT :: Binary_Tree (char *List, int Lower, int Upper)
{
NODE *Node;
int Mid = (Lower + Upper)/2;
Node = new (NODE);

Node->Info = List [Mid];
if ( Lower>= Upper)
{
Node->Left_Child = NULL;
Node->Right_Child = NULL;
return (Node);
}

if (Lower <= Mid - 1)
Node->Left_Child = Binary_Tree (List, Lower, Mid - 1);
else
Node->Left_Child = NULL;
if (Mid + 1 <= Upper)
Node->Right_Child = Binary_Tree (List, Mid + 1, Upper);
else
Node->Right_Child = NULL;
return(Node);
}

// Output function

void Create_BT :: Output(NODE *T, int Level)
{
if (T)
{
Output(T->Right_Child, Level+1);
cout<<"\n";
for (int i = 0; i < Level; i++)
cout<<" ";
cout<< T->Info;
Output(T->Left_Child, Level+1);
}
}

// Function main

void main()
{
Create_BT Binary_C_Tree;
char List[100];
int Number = 0;
char Info ;
char choice;
NODE *T = new (NODE);
T = NULL;
cout<<"\n Input choice 'b' to break:";
choice = getche();
while(choice != 'b')
{
cout<<"\n Input information of the node: ";
cin>>Info;
List[Number++] = Info;
cout<<"\n Input choice 'b' to break:";
choice = getche();
}
Number --;
cout<<"\n Number of elements in the lsit is "<<Number;
T = Binary_C_Tree.Binary_Tree(List, 0, Number);
Binary_C_Tree.Output(T,1);
}

Is This Answer Correct ?    5 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is class??

748


What are the states of thread in java?

507


What is the biggest integer?

543


Which container method is used to cause a container to be laid out and redisplayed in java programming?

591


What is the difference between pass by reference and pass by pointer?

489






What the difference is between execute, execute Query, execute Update?

366


What is variable declaration and definition?

536


Under what conditions is an object’s finalize() method invoked by the garbage collector?

573


What is a "pure virtual" member function?

594


Is there a sort function in java?

571


What do you mean by inner class in java?

561


How can we avoid including a header more than once?

603


What are generic methods?

523


What does sprintf mean?

525


What is final int?

522