Write a program to create a binary Tree ?
Answers were Sorted based on User's Feedback
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 |
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 |
Can I use % with real numbers?
What is difference between == equals () and compareto () method?
What is means by DLL file means ? What is the use of DLL file? What are the contents of DLL file?
How do we access static members in java?
Explain the difference between intermediate and terminal operations in java8?
Can we override static methods in java?
What is hypertext?
Explain Basics of OOP Language in java
What are checked exceptions?
What is string and its types?
I Have a class abstract with one abstract method, so that method should override in the subclass, but i dont want to override, if i am not override what will happen? If compilation will occur then i dont want to give compilation error, then what we need to do??? See the sample program. public abstract class AbstractExample { public abstract void sampleMethod(); } public class AbstractExampleImple extends AbstractExample { }
What are synchronized blocks in java?