1 / 4

Suggested Questions

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

armani
Download Presentation

Suggested Questions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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))); }

  2. 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); }

  3. 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); }

  4. 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.

More Related