Copy verbatim the code and complete the Implementation of the Binary Tree below, the instructions are written as comments:
For iterative version of the tree traversal, you will need a TreeQueue and TreeStack Objects.
TreeQueue is a Queue where the item enqueued and dequeued are TreeNodes object
TreeStack is a stack where the items pushed and popped are TreeNodes Object
#include<iostream.h>
#include "TreeQueue.cpp"
#include "TreeStack.cpp"
typedef struct TreeNode{
int key;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
TreeNode(int data){
key=data;
left=right=parent=NULL;
}
int isRoot(); // return TRUE if the node is root, otherwise FALSE
int isLeaf(); // return TRUE if the node is leaf, otherwise FALSE
int isAncestor(TreeNode *tree); // return TRUE if the node is an ancestor of tree(parameter)
void preorder(); // display preorder listing(using recursion)
void preorderV2(); // display preorder listing(using iteration or while statement code already given)
void levelorder(); // display levelorder listing
void postorder(); // display postorder listing (using recursion)
void postorderV2(); // display postorder listing(using iteration or while statement code already given)
void inorder(); // display inorder listing(using recursion)
int getCount(); // return the number of descendant nodes at any node
int getLevel(); // return the level number of a node e.g. root is level 1
int getHeight(); // return the height of a node e.g. root with no child height is 0
void setLeft(int data);
void setRight(int data);
};
void TreeNode::setLeft(int data){
}
void TreeNode::setRight(int data){
}
int TreeNode::isRoot(){
}
int TreeNode::isLeaf(){
}
void TreeNode:reorder(){
}
void TreeNode::levelorder(){
TreeQueue *queue = new TreeQueue();
queue->enqueue(this);
while (!queue->empty()){
cout<<queue->front->key <<", ";
if (queue->front->left!=NULL)
queue->enqueue(queue->front->left);
if (queue->front->right)
queue->enqueue(queue->front->right);
queue->dequeue();
}
}
TreeNode:reorderV2(){
StackTree *stack=new StackTree();
stack->push(this);
while(!stack->empty()){
TreeNode *top = stack->top;
stack->pop();
cout<<top->key ;
if (top->right!=NULL) stack->push(top->right);
if (top->left!=NULL) stack->push(top->left);
}
}
TreeNode:ostorderV2(){
TreeStack *s1=new TreeStack();
TreeStack *s2=new TreeStack();
s1->push(this);
while(!s1->empty()){
TreeNode *tmp=s1->top;
s2->push(tmp);
s1->pop();
if (tmp->left!=NULL) s1->push(tmp->left);
if (tmp->right!=NULL) s1->push(tmp->right);
}
while(!s2->empty()){
cout<<s2->top->key;
s2->pop();
}
}
// *********** Sample Main *************//
void main(){
TreeNode *root=new TreeNode(30);
root->setLeft(10); root->setRight(6);
root->left->setLeft(15); root->left->setRight(26);
root->right->setLeft(7); root->right->setRight(4);
root->right->left->setLeft(8); root->right->left->setRight(12);
TreeNode *sample=root->left->right
cout<<"Recursive Implementation: \n";
cout<<"\nPreorder: "; root->preorder();
cout<<"\nInorder: "; root->inorder();
cout<<"\nPostorder: "; root->postorder();
cout<<"\nLevel: "; root->getLevel();
cout<<"\nNo of Nodes: "; root->getCount();
cout<<"\nHeight of Node root: "<< root->getHeight();
cout<<"\n"<<sample->key<<" is Root: "<< sample->isRoot();
cout<<"\n"<<sample->key<<" is leaf: "<< sample->isLeaf();
cout<<"\n"<<root->key<<" is ancestor of "<< sample->key<<" "<<root->isAncestor(sample);
cout<<"\n\nIterative Version of order listing";
cout<<"\nLevel Order: "; root->levelorder();
cout<<"\nPre Order: "; root->preorderV2();
cout<<"\nPost Order: "; root->postorderV2();
}
yan po ang problem namen..pa help..
For iterative version of the tree traversal, you will need a TreeQueue and TreeStack Objects.
TreeQueue is a Queue where the item enqueued and dequeued are TreeNodes object
TreeStack is a stack where the items pushed and popped are TreeNodes Object
#include<iostream.h>
#include "TreeQueue.cpp"
#include "TreeStack.cpp"
typedef struct TreeNode{
int key;
TreeNode *left;
TreeNode *right;
TreeNode *parent;
TreeNode(int data){
key=data;
left=right=parent=NULL;
}
int isRoot(); // return TRUE if the node is root, otherwise FALSE
int isLeaf(); // return TRUE if the node is leaf, otherwise FALSE
int isAncestor(TreeNode *tree); // return TRUE if the node is an ancestor of tree(parameter)
void preorder(); // display preorder listing(using recursion)
void preorderV2(); // display preorder listing(using iteration or while statement code already given)
void levelorder(); // display levelorder listing
void postorder(); // display postorder listing (using recursion)
void postorderV2(); // display postorder listing(using iteration or while statement code already given)
void inorder(); // display inorder listing(using recursion)
int getCount(); // return the number of descendant nodes at any node
int getLevel(); // return the level number of a node e.g. root is level 1
int getHeight(); // return the height of a node e.g. root with no child height is 0
void setLeft(int data);
void setRight(int data);
};
void TreeNode::setLeft(int data){
}
void TreeNode::setRight(int data){
}
int TreeNode::isRoot(){
}
int TreeNode::isLeaf(){
}
void TreeNode:reorder(){
}
void TreeNode::levelorder(){
TreeQueue *queue = new TreeQueue();
queue->enqueue(this);
while (!queue->empty()){
cout<<queue->front->key <<", ";
if (queue->front->left!=NULL)
queue->enqueue(queue->front->left);
if (queue->front->right)
queue->enqueue(queue->front->right);
queue->dequeue();
}
}
TreeNode:reorderV2(){
StackTree *stack=new StackTree();
stack->push(this);
while(!stack->empty()){
TreeNode *top = stack->top;
stack->pop();
cout<<top->key ;
if (top->right!=NULL) stack->push(top->right);
if (top->left!=NULL) stack->push(top->left);
}
}
TreeNode:ostorderV2(){
TreeStack *s1=new TreeStack();
TreeStack *s2=new TreeStack();
s1->push(this);
while(!s1->empty()){
TreeNode *tmp=s1->top;
s2->push(tmp);
s1->pop();
if (tmp->left!=NULL) s1->push(tmp->left);
if (tmp->right!=NULL) s1->push(tmp->right);
}
while(!s2->empty()){
cout<<s2->top->key;
s2->pop();
}
}
// *********** Sample Main *************//
void main(){
TreeNode *root=new TreeNode(30);
root->setLeft(10); root->setRight(6);
root->left->setLeft(15); root->left->setRight(26);
root->right->setLeft(7); root->right->setRight(4);
root->right->left->setLeft(8); root->right->left->setRight(12);
TreeNode *sample=root->left->right
cout<<"Recursive Implementation: \n";
cout<<"\nPreorder: "; root->preorder();
cout<<"\nInorder: "; root->inorder();
cout<<"\nPostorder: "; root->postorder();
cout<<"\nLevel: "; root->getLevel();
cout<<"\nNo of Nodes: "; root->getCount();
cout<<"\nHeight of Node root: "<< root->getHeight();
cout<<"\n"<<sample->key<<" is Root: "<< sample->isRoot();
cout<<"\n"<<sample->key<<" is leaf: "<< sample->isLeaf();
cout<<"\n"<<root->key<<" is ancestor of "<< sample->key<<" "<<root->isAncestor(sample);
cout<<"\n\nIterative Version of order listing";
cout<<"\nLevel Order: "; root->levelorder();
cout<<"\nPre Order: "; root->preorderV2();
cout<<"\nPost Order: "; root->postorderV2();
}
yan po ang problem namen..pa help..