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
#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;
}
Comments
Leave a comment