Write a program to create a binary Tree ?

Answers were Sorted based on User's Feedback



Write a program to create a binary Tree ?..

Answer / 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

Write a program to create a binary Tree ?..

Answer / marshal

public class BinaryTree
extends AbstractTree
{
protected Object key;
protected BinaryTree left;
protected BinaryTree right;

public BinaryTree (
Object key, BinaryTree left, BinaryTree right)
{
this.key = key;
this.left = left;
this.right = right;
}

public BinaryTree ()
{ this (null, null, null); }

public BinaryTree (Object key)
{ this (key, new BinaryTree (), new BinaryTree()); }
// ...
}

Is This Answer Correct ?    7 Yes 12 No

Post New Answer

More Core Java Interview Questions

What is a finally block?

0 Answers  


What is the difference between reader/writer and inputstream/output stream?

0 Answers  


what are Hostile Applets?

0 Answers  


What is treemap in java?

0 Answers  


What is the need to implement Serializable interface (with no methods) for objects which are to be serialized ? We can write our own functionality which writes objects to streams then why we need to implement and tell JVM that which objects can be serialized.

6 Answers   iFlex, Sapient,






What is the purpose of the enableevents() method in java programming?

0 Answers  


what is the purpose of using rmisecuritymanager in rmi?

0 Answers  


What is serial version uid and its importance in java?

0 Answers  


What is bean? Where it can be used?

0 Answers  


how a programmer confirms that the data submitted has been succesfully inserted into the database(either oracle or my sql).. How a programmer confirm if there is any problem with the program he wrote for insertion... ANS:--- >executeupdate method is having boolean return type, if anything goes wrong in data insertion or data updation, it would return false. otherwise, if it successfully inserts data into the database, it would return true NOW HOW TO I CHECK IN MY DURING EXECUTION WHETHER IT RETURNS TRUE OR FALSE... WELL IT WILL DISPLAY ANY MESSAGE OR NOT

0 Answers   Google,


what is java bean?where can we use it?

12 Answers   TCS,


What is the meaning of course?

0 Answers  


Categories