Answer to Question #240782 in C for FAJLA

Question #240782

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


1
Expert's answer
2021-09-22T23:49:22-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