Sei sulla pagina 1di 11

Trees programs consolidated 1) Give the class definition of a node of a binary tree.

class btreenode { public: btreenode *leftchild ; int data ; btreenode *rightchild ; }; 2) Write a program to construct a binary search tree recursively. void btree :: buildtree ( int num ) { insert ( &root, num ) ; } void btree :: insert ( btreenode **sr, int num ) { if ( *sr == NULL ) { *sr = new btreenode ; ( *sr ) -> leftchild = NULL ; ( *sr ) -> data = num ; ( *sr ) -> rightchild = NULL ; return ; } else // search the node to which new node will be attached { // if new data is less, traverse to left if ( num < ( *sr ) -> data ) insert ( & ( ( *sr ) -> leftchild ), num ) ; else // else traverse to right insert ( & ( ( *sr ) -> rightchild ), num ) ; } return ; } 3) Write recursive routines for postorder, inorder and preorder traversals of a binary tree.

void btree :: traverse( ) { cout << "\nIn-order Traversal: " ; inorder ( root ) ; cout << "\nPre-order Traversal: " ; preorder ( root ) ; cout << "\nPost-order Traversal: " ; postorder ( root ) ; } void btree :: inorder ( btreenode *sr ) { if ( sr != NULL ) { inorder ( sr -> leftchild ) ; cout << "\t" << sr -> data ; inorder ( sr -> rightchild ) ; } } void btree :: preorder ( btreenode *sr ) { if ( sr != NULL ) { cout << "\t" << sr -> data ; preorder ( sr -> leftchild ) ; preorder ( sr -> rightchild ) ; } } void btree :: postorder ( btreenode *sr ) { if ( sr != NULL ) { postorder ( sr -> leftchild ) ; postorder ( sr -> rightchild ) ; cout << "\t" << sr -> data ; } } 4) Write a program to count all leaves in a tree.

static int leafcount = 0; void Tree::countleaves1() { countleaves2(root); cout<<"TOTAL LEAVES ARE = " <<leafcount; } void Tree::countleaves2(node *p) { if(p != NULL) { if(p->left == NULL && p->right == NULL) leafcount++; countleaves2(p->left); countleaves2(p->right); } } 5) Write a program to delete all leaf nodes in a binary tree. void Tree::del1() { if( root == NULL) //No node in tree hence return return; if( root->left == NULL && root->right == NULL) //root is the only node hence a leaf root = NULL; else { del2(root->left,root); //visit left subtree of root del2(root->right,root); //visit right subtree of root } } void Tree::del2(node *p,node *prev) { if(p != NULL) { if(p->left != NULL || p->right != NULL) //clearly if this holds current p is not a leaf { prev = p; //store it in previous del2(p->left,prev); //visit left subtree del2(p->right,prev); //visit right subtree

} else del3(p,prev); } }

//p must be a leaf hence delete

void Tree::del3(node *p, node *prev) //simple function { if(p->left == NULL && p->right == NULL) { if(prev->left == p) { prev->left = NULL; delete p; } else { prev->right = NULL; delete p; } } } OR [ courtesy Yatin Sarabalia ] void Del() { if(Root==NULL) { cout<<"No nodes"; return; } if(Root->Left==NULL&&Root->Right==NULL) { Root=NULL; return; } Delete(&Root,&Root); } void Delete(Node **p,Node **prev) { if((*p)!=NULL) { if((*p)->Left==NULL&&(*p)->Right==NULL) { if((*prev)->Left==(*p))

(*prev)->Left=NULL; else if((*prev)->Right==(*p)) (*prev)->Right=NULL; delete (*p); } Delete(&((*p)->Left),&(*p)); Delete(&((*p)->Right),&(*p)); } } } 5) Write a program to find the depth of a tree recursively. int leftx; int rightx; void btree::LeftDepth() { if(root->leftchild != NULL) leftx = TreeDepth(root->leftchild); //measure the left sub-tree } void btree::RightDepth() { if(root->rightchild != NULL) rightx = TreeDepth(root->rightchild); //measure the right sub-tree } int btree::TreeDepth(btreenode *q) { int left = 0; int right = 0; if(q->leftchild != NULL) left = TreeDepth(q->leftchild); //get the depth of the left sub-tree if(q->rightchild != NULL) right = TreeDepth(q->rightchild); //get the depth of the right sub-tree if( left > right) //check to see which sub-tree is deeper return left+1; //return the depth of the left sub-tree plus 1 else return right+1; //return the depth of the right sub-tree plus 1 }

6) Write a program to delete a complete binary tree. btree :: ~btree( ) { del ( root ) ; } // deletes nodes of a binary tree void btree :: del ( btreenode *sr ) { if ( sr != NULL ) { del ( sr -> leftchild ) ; del ( sr -> rightchild ) ; } delete sr ; } 7) Write a program to create a mirror image of a tree. void btree::exchange1() { exchange2(root); } void btree::exchange2(btreenode *p) { if(p->leftchild != NULL || p->rightchild != NULL) { btreenode *temp; temp = p->leftchild ; p->leftchild = p->rightchild ; p->rightchild = temp; if(p->leftchild != NULL) exchange2(p->leftchild); if(p->rightchild != NULL) exchange2(p->rightchild); } } 8) Write a program to create a copy of a binary tree.

void btree::copy1() { if(root != NULL) { head = new btreenode; head->data = root->data; head->leftchild = NULL; head->rightchild = NULL; } else return; copy2(root,head); //copy the tree except the root which is already copied } void btree::copy2(btreenode *p,btreenode *head1) { if(p->leftchild != NULL) { btreenode *temp = new btreenode; temp->data = p->leftchild->data; head1->leftchild = temp; temp->leftchild = NULL; temp->rightchild = NULL; } if(p->rightchild != NULL) { btreenode *temp = new btreenode; temp->data = p->rightchild->data; head1->rightchild = temp; temp->leftchild = NULL; temp->rightchild = NULL; } if(p->leftchild != NULL) copy2(p->leftchild, head1->leftchild); if(p->rightchild != NULL) copy2(p->rightchild, head1->rightchild); }

9) Write an iterative routine to insert a number in a binary search tree. void Tree::insert(int x) //non-recursive insert function { node *temp = root; if(temp == NULL) { temp = new node; temp->info = x; temp->left = NULL; temp->right = NULL; root = temp; } else { while(1) { if(x < temp->info) //taking the left subtree if(temp->left != NULL) temp = temp->left; else { temp->left = new node; temp->left->info = x; temp->left->left = NULL; temp->left->right = NULL; break; } if(x > temp->info) //taking the right subtree if(temp->right != NULL) temp = temp->right; else { temp->right = new node; temp->right->info = x; temp->right->left = NULL; temp->right->right = NULL; break; } } } }

10) Write a program to count all nodes in a binary tree. void Tree::totalnodes1() { cout<<totalnodes2(root); } int Tree::totalnodes2(node *p) { static int count = 0; if(p != NULL) { count++; totalnodes2(p->left); totalnodes2(p->right); } return(count); } 11) Write a program to find if a tree is a binary search tree or not. void Tree:: binary1() { binary2(root); } void Tree:: binary2(node *p) { if(p != NULL) { if(p->left != NULL) if(p->left->info > p->info) { cout<<"not a binary tree"; return; } if(p->right != NULL) if(p->right->info < p->info) { cout<<"not a binary tree"; return;

} binary2(p->left); binary2(p->right); } } 12) Write the pseudo-code for iterative in-order traversal of a binary tree. Assuming a stack is represented by s in the following code and the root node is p. void inorder (node *p) { do { while (p != NULL) { push(s,p); //push p into stack s p = p left; } if ( !empty(s) ) ///if s is not empty { p = pop(s) ; // get first item from s into p visit(p) ; // some function p = pright; } } while ( p != NULL || (!empty(s) ) ; } 12) Write the pseudo-code for iterative pre-order traversal of a binary tree. void preorder( node *p) { do { while( p != NULL) { visit(p); push(s,p); p = pleft; } if( !empty(s) ) { p = pop(s); p = pright; }

} while( p!= NULL || (!empty(s) ) ) } 13) Write the pseudo-code for iterative in-order traversal of a binary tree. (This I have taken from Drozdek) void postorder() { node *p = root, *q = root; while ( p != NULL) { while( pleft != NULL) { push(s,p); p = pleft; } while(p != NULL && (pright == NULL || pright ==q) { visit(p); q = p; if( empty (s) ) return; p = pop(s); } push(s,p); p = pright; } }