Question: 1. Construct a binary tree, print the elements of the tree (preorder, postorder, inorder) and compute the size of the tree. 2. Construct a binary

1. Construct a binary tree, print the elements of the tree (preorder, postorder, inorder) and compute the size of the tree.

2. Construct a binary search tree

I. Display inorder form

II. Search an element in BST

III. Insert a node

 1. Construct a binary tree, print the elements of the tree

(preorder, postorder, inorder) and compute the size of the tree. 2. Construct

a binary search tree I. Display inorder form II. Search an element

1 2 3 #include #include 4 5 6 7 8 9 10 11 12 13 struct node { int data; struct node *rightchild; struct node *leftchild; }; int x) struct node* searchdata (struct node *root, { I write your code here } struct node* new_node(int x) { struct node *temp; temp = malloc(sizeof(struct node)); temp->data = x; temp->leftchild = NULL; temp->rightchild = NULL; return temp; } 15 16 77 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 int x) struct node* insert(struct node *root, { //write your code here } struct node* findminimum(struct node *root) { if(root == NULL) return NULL; else if(root->leftchild != NULL) // node with minimum value will have no left child return findminimum(root->leftchild); // left most element will be minimum return root; 38 39 40 } struct node * deletenode (struct node *root, int x) { //find if the element to be deleted is present in the tree if(root==NULL) return NULL; if (x>root->data) root->rightchild = deletenode(root->rightchild, x); else if(xdata) root->leftchild = deletenode (root->leftchild, x); else { // 1st case is no children if(root->leftchild==NULL && root->rightchild==NULL) { free(root); return NULL; } 1/2nd case is node to be deleted has one child. else if(root->leftchild==NULL || root->rightchild==NULL) { struct node *temp; if(root->leftchild==NULL) temp = root->rightchild; // put the child in temporary variable else temp = root->leftchild; free(root); return temp; } 1/3rd case is the node to be deleted has else { struct node *temp = findminimum(root->rightchild); root->data = temp->data; root->rightchild = deletenode (root->rightchild, temp->data); void inorder(struct node *root) //inorder display { if(root!=NULL) { inorder(root->leftchild); printf("%d", root->data); inorder (root->rightchild); } } int main() { struct node *root; struct node* tmp; 86 87 88 89 90 91 92 93 94 95 96 97 98 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 root = new_node(6); insert(root,3); insert(root, 10); insert(root,1); insert(root,5); insert(root, 4); tmp = searchdata(root, 5); if (tmp) { printf("Searched node=%d ", tmp->data); } else { printf("Data Not found in tree. "); } inorder(root); printf(" "); root= deletenode (root, 10); inorder(root); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!