Answer to Question #240221 in C for jonny

Question #240221

9. Implement a BST-Binary Search Tree (for integer numbers) that consists of following

operations:

(1) Insert

(2) Tree minimum

(3) Search

(4) In-order traversal

(5) Tree successor

(6) Delete

[Make it Menu driven: 1 means insert, 2 means tree minimum and so on….]



1
Expert's answer
2021-09-22T00:02:29-0400
#include <stdio.h>
#include <stdlib.h>


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


struct node* newNode(int item)
{
	struct node* temp
		= (struct node*)malloc(sizeof(struct node));
	temp->k = item;
	temp->l = temp->r = NULL;
	return temp;
}


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* r = NULL;
	r = insert(r, 60);
	insert(r, 40);
	insert(r, 30);
	insert(r, 50);
	insert(r, 80);
	insert(r, 70);
	insert(r, 90);


	inorder(r);


	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