40 likes | 130 Views
Suggested Questions. Write a function to count the number of nodes in a given tree struct tnode { int data; struct tnode *lchild, *rchild; }; int count(struct tnode *p) { if( p == NULL) return(0); else
E N D
Suggested Questions • Write a function to count the number of nodes in a given tree struct tnode { int data; struct tnode *lchild, *rchild; }; int count(struct tnode *p) { if( p == NULL) return(0); else if( p->lchild == NULL && p->rchild == NULL) return(1); else return(1 + (count(p->lchild) + count(p->rchild))); }
Suggested Questions • Write a program to swap the left and right subtree in a given binary struct tnode *swaptree(struct tnode *p) { struct tnode *temp1=NULL, *temp2=NULL; if( p != NULL) { temp1= swaptree(p->lchild); temp2 = swaptree(p->rchild); p->rchild = temp1; p->lchild = temp2; } return(p); }
Suggested Questions • A function to serch for a given data value in a binary search tree struct tnode *search( struct tnode *p,int key) { struct tnode *temp; temp = p; while( temp != NULL) { if(temp->data == key) return(temp); else if(temp->data > key) temp = temp->lchild; else temp = temp->rchild; } return(NULL); }
Suggested Questions • Draw expression trees for each of the following expressions, and show the order of visiting the vertices in (1) preorder, (2) inorder, and (3) postorder: (a) logn! (b) .a − b.−c (c) a − .b − c. (d) .a < b. and .b < c. and .c < d.