Answer to Question #272512 in C for fgsgd6

Question #272512

WAP Write the following menu driven program for the binary search tree

----------------------------------------

Binary Search Tree Menu

----------------------------------------

1. Create

2. In-Order Traversal

3. Pre-Order Traversal

4. Post-Order traversal

5. Search

6. Find Smallest Element

7. Find Largest Element

8. Deletion of Tree

9. Quit


1
Expert's answer
2021-12-02T00:34:57-0500
#include <stdio.h>
#include <stdlib.h>


struct node {
	int k;
	struct node *l, *r;
};


struct node* newNode(int x)
{
	struct node* tmp= (struct node*)malloc(sizeof(struct node));
	tmp->k = x;
	tmp->l = tmp->r = NULL;
	return tmp;
}
void inorder(struct node* root)
{
	if (root != NULL) {
		inorder(root->l);
		printf("%d \n", root->k);
		inorder(root->r);
	}
}


struct node* insert(struct node* node, int k)
{
	if (node == NULL)
		return newNode(k);
	if (k < node->k)
		node->l = insert(node->l, k);
	else if (k > node->k)
		node->r = insert(node->r, k);
	return node;
}


int main()
{
	struct node* root = NULL;
	root = insert(root, 40);
	insert(root, 10);
	insert(root, 20);
	insert(root, 30);
	insert(root, 50);
	insert(root, 60);
	insert(root, 70);
	inorder(root);


	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS