Binary Tree Search

The Binary Tree Search

Binary tree search adalah sebuah data struktur turunan dari Tree data struktur yang berfungsi sebagai penyimpan data secara berurut, dimana setiap node mempunyai anak anak dimana anak kiri dari node memiliki nilai yang lebih kecil dari nilai pada parent dan untuk node kanan, nilainya lebih besar dari node parent.
Berikut ini adalah codingan yang saya buat untuk BST ini:

#include<stdio.h>
#include<stdlib.h>
 
struct node
{
    int number;
    struct node *leftChild, *rightChild;
};

int menu()
{
int choice;
printf("Binary Tree Search Program\n");
printf("==========================\n");
printf("1.Add Node\n");
printf("2.Print Node\n");
printf("3.Delete Node\n");
printf("4.Exit Program\n");
printf("==========================\n");
printf(">> ");
scanf("%d", &choice);
return choice;
}

struct node *newNode(int item)
{
    struct node *temp =  (struct node *)malloc(sizeof(struct node));
    temp->number = item;
    temp->leftChild = temp->rightChild = NULL;
    return temp;
}

struct node* FindNode(struct node* root, int number)
{
    if (root == NULL || root->number == number)
       return root;
   
    if (root->number < number)
       return FindNode(root->rightChild, number);

    return FindNode(root->leftChild, number);
}

struct node * FindLeastValue(struct node* node)
{
    struct node* current = node;

    while (current && current->leftChild != NULL)
        current = current->leftChild;

    return current;
}

struct node* RemoveNode(struct node* root, int number)
{
    if (root == NULL) return root;

    if (number < root->number)
        root->leftChild = RemoveNode(root->leftChild, number);

    else if (number > root->number)
        root->rightChild = RemoveNode(root->rightChild, number);

    else
    {
        if (root->leftChild == NULL)
        {
            struct node *temp = root->rightChild;
            free(root);
            return temp;
        }
        else if (root->rightChild == NULL)
        {
            struct node *temp = root->leftChild;
            free(root);
            return temp;
        }

        struct node* temp = FindLeastValue(root->rightChild);

        root->number = temp->number;

        root->rightChild = RemoveNode(root->rightChild, temp->number);
    }
    return root;
}

void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->leftChild);
        printf("%d \n", root->number);
        inorder(root->rightChild);
    }
}
 
struct node* AddNode(struct node* node, int number)
{
    if (node == NULL) return newNode(number);

    if (number < node->number)
        node->leftChild  = AddNode(node->leftChild, number);
    else if (number > node->number)
        node->rightChild = AddNode(node->rightChild, number); 

    return node;
}
 
int main()
{
int choice;
struct node *root = NULL;
do{
choice = menu();
switch(choice){
case 1:{
int input;
printf("Input Number To Be Added : ");
scanf("%d",&input);
root = AddNode(root,input);
break;
}
case 2:{
inorder(root);
break;
}
case 3:{
int input;
printf("Input Number To Be Deleted : ");
scanf("%d",&input);
root = RemoveNode(root,input);
break;
}
}
   
   }while(choice != 4);
    inorder(root);
    return 0;

Komentar

Postingan populer dari blog ini

Heap (Max And Min) & Tries

Linked List : Is it an Array?

Hash and Trees