Suppose you have the following code:
void InsertNode(tNode** node, int i){
if(*node == NULL){
*node = new tNode;
(*node)->pLeft = NULL;
(*node)->data = i;
(*node)->pRight = NULL;
SetRootNode(node);
return;
} else {
if(i < (*node)->data)
InsertNode(&((*node)->pLeft), i);
if(i > (*node)->data)
InsertNode(&((*node)->pRight), i);
return;
}
}
void Func(tNode **node){
if(*node!=NULL){
Func(&(*node)->pLeft);
tNode *temp;
temp = (*node)->pLeft;
(*node)->pLeft= (*node)->pRight;
(*node)->pRight = temp;
Func(&(*node)->pRight);
}
}
void traverse(tNode** nd){
if(*nd!=NULL){
traverse(&((*nd)->pLeft));
traverse(&((*nd)->pRight));
std::cout<<(*nd)->data<<std::endl;
}
}
Let the input given be
98,15,100,10,78,120,5,12,96,110
What would be the output of the following code snippet?
int main(void) {
tree *bT = new tree;
int i = 10;
int data;
while(i--){
std::cout<<"Enter the node"<<std::endl;
std::cin>>data;
bT->InsertNode(bT->GetRootNode(), data);
}
bT->Func(bT->GetRootNode());
bT->InsertNode(bT->GetRootNode(), 99);
bT->Func(bT->GetRootNode());
bT->traverse(bT->GetRootNode());
}
Options
-5,10,12,15,78,96,98,99,100,1
10,120
-5,12,10,99,96,78,15,110,120,
100,98
-5,10,12,15,78,96,99,98,100,1
10,120
-98,100,120,110,15,78,96,99,1
0,12,5
-5,12,10,99,96,78,15,110,120,
- HarishIITR January 11, 2012100,98
Its just making the right node to left node and left to right of root only..
final tree:
98
15 100
10 78 120
5 12 96 110
99