MODUL 7
BINARY SEARCH TREE
Pada tabel berikut ditunjukkan kode BST.cpp.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
//========================================================
struct node { int data;
struct node *right;
struct node *left;
};
typedef struct node node;
node *createNode(int bil);
void insert(node **root, node *newNode);
void inorder(node *root);
node *findLargestBST(node *root);
bool deleteNode(node **root, int key);
void deleteBST(node **root);
//========================================================
void main() {
node *root, *pNew;
char pilih;
int bil;
bool success;
root = NULL;
do{
clrscr();
printf("masukkan pilihan\n");
printf("1. tambah data\n");
printf("2. hapus data\n");
printf("3. cetak isi tree secara inorder\n");
printf("MASUKKAN PILIHAN (tekan q untuk keluar) : ");
fflush(stdin);
scanf("%c", &pilih);
if (pilih == '1'){
printf("nilai yang akan dimasukkan ke tree: ");
scanf("%d", &bil);
pNew = createNode(bil);
if (pNew == NULL){
printf("insert gagal");
getch();
} else
insert(&root, pNew);
}
else if (pilih == '2'){
printf("nilai yang akan dihapus : ");
scanf("%d", &bil);
success = deleteNode(&root, bil);
if(!success){
printf("\ndata gagal dihapus");
getch();
} }
else if (pilih == '3'){
inorder(root);
getch();
}
} while (pilih != 'q');
deleteBST(&root);
}
//========================================================
node *createNode(int bil){
node *pNew;
pNew = (node *)malloc(sizeof(node));
if (pNew != NULL){
pNew->data = bil;
pNew->left = NULL;
pNew->right = NULL;
}
return(pNew);
}
void insert(node **root, node *newNode){
}
void inorder(node *root){
if (root != NULL){
inorder(root->left);
printf(" %d,", root->data);
inorder(root->right);
} }
node *findLargestBST(node *root){
if (root ->right == NULL) return(root);
else
return(findLargestBST(root->right));
}
bool deleteNode(node **root, int key){
}
void deleteBST(node **root){
while(*root != NULL)
deleteNode(root, (*root)->data);
}