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 are the advantages and disadvantages of reference counting in garbage collection?

579


What is independent and dependent variables in research?

472


Can you explain the final method modifier?

571


Is it possible to use Semaphore/ Mutex in an Interrupt Handler?

538


23. Storage space in java is of the form Stack Queue Heap List 24. What is java code embedded in a web page known as Applets Servlets scriptlets snippets 25. Which of the following attributes are compulsory with an tag?. code,height & width. 26. What does 'CODEBASE' in an applet tag specify?. Files absolute path.

2063






What is boolean false?

531


Does constructor be static?

567


What is ide with example?

549


What is the difference between length and size in java?

501


When is finally block not called?

584


What is update method called?

666


Can we create a constructor in abstract class?

575


What is unmodifiable collection in java?

514


What is data object example?

535


What do you understand by weak reference?

561