Binary Search Tree

Binary Search Tree
adalah sebuah data structure binary tree yang memiliki ciri-ciri, yaitu: subtree sebelah kiri nilainya lebih kecil dari parent nodenya dan subtree sebelah kanan nilainya lebih besar dari parent nodenya.



Terdapat 3 cara dalam traverse binary tree (dilakukan secara rekursi):

  • In-order
Yaitu subtree kiri dilalui terlebih dahulu lalu ke root dan ke subtree kanan.
  • Pre-order
Yaitu root yang dilalui terlebih dahulu lalu subtree kiri dan kanan.
  • Post-order
Yaitu subtree kiri dan kanan dilalui terlebih dahulu lalu root.


Berikut adalah contoh Binary Search Tree :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

struct Node{
int id;
char name[100];
struct Node *left, *right;
};

struct Node *createNode(int id, char name[]){
struct Node *newNode = (Node*)malloc(sizeof(Node));
newNode->id = id;
strcpy(newNode->name, name);
newNode->left = newNode->right = NULL;
return newNode;
}

struct Node *insertNode(struct Node *currNode, int id, char name[]){
if(currNode == NULL){
return createNode(id, name);
}
if(currNode->id > id){
currNode->left = insertNode(currNode->left, id, name);
}
else if(currNode->id < id){
currNode->right = insertNode(currNode->right, id, name);
}
return currNode;
}

void printInfix(struct Node *currNode){
if(currNode == NULL){
return;
}
printInfix(currNode->left);   
printf("%-5d - %-20s\n",currNode->id, currNode->name);    
printInfix(currNode->right);
}

void printPrefix(struct Node *currNode){
if(currNode == NULL){
return;
}
printf("%-5d - %-20s\n",currNode->id, currNode->name);
printPrefix(currNode->left);
printPrefix(currNode->right);
}

void printPostfix(struct Node *currNode){
if(currNode == NULL){
return;
}
printPostfix(currNode->left);
printPostfix(currNode->right);
printf("%-5d - %-20s\n",currNode->id, currNode->name);
}

struct Node *predecessor(struct Node *currNode){
currNode = currNode->left;
while(currNode->right){
currNode = currNode->right;
}
return currNode;
}

struct Node *deleteNode(struct Node *currNode, int id){
if(currNode == NULL){
return NULL;
}
if(currNode->id > id){
currNode->left = deleteNode(currNode->left, id);
}
else if(currNode->id < id){
currNode->right = deleteNode(currNode->right, id);
}
else{
if(currNode->left == NULL || currNode->right == NULL){
struct Node *tempNode = NULL;
if(currNode->left != NULL){
tempNode = currNode->left;
}
else{
tempNode = currNode->right;
}
if(tempNode == NULL){ // kalau tidak ada anak.
tempNode = currNode;
currNode = NULL;
free(tempNode);
}
else{ // kalau anaknya 1.
*currNode = *tempNode;
free(tempNode);
}  
}
else{
struct Node *tempNode = predecessor(currNode);
currNode->id = tempNode->id;  // copy data dari temp ke curr.
strcpy(currNode->name, tempNode->name);
currNode->left = deleteNode(currNode->left, tempNode->id);
}
}
return currNode;
}

int search(struct Node *currNode, int id)
{
if(currNode == NULL)
return 0;
printInfix(currNode->left);
if(currNode->id == id)
return 1;
printInfix(currNode->right);
}

int main(){
struct Node *root = NULL;
root = insertNode(root, 20, "Bambang");
root = insertNode(root, 24, "Michelle");
root = insertNode(root, 22, "Budi");
root = insertNode(root, 18, "Jose");
root = insertNode(root, 18, "Yani"); // kalau sama tidak di print.
printf("Infix :\n");
printInfix(root);printf("\n");
printf("Postfix :\n");
printPostfix(root);printf("\n");
printf("Prefix :\n");
printPrefix(root);printf("\n");
printf("Delete(Pop) :\n");
root = deleteNode(root, 22);
printInfix(root);printf("\n");
printf("Search() :\n");
if(search(root, 18) == 0)
{
printf("Not Found");
}
else 
{
printf("Found");
}

}

Comments

Popular posts from this blog

Heap & Tries

Linked List II

AVL Tree