#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
struct tree_node
{
tree_node* left;
tree_node* right;
int data;
};
tree_node* root;
bool isEmpty()
{return root==NULL;}
void insert(int d)
{
tree_node* t = new tree_node;
tree_node* parent;
t->data = d;
t->left = NULL;
t->right = NULL;
parent = NULL;
if(isEmpty())root = t;
else
{
tree_node* curr;
curr = root;
while(curr!=NULL)
{
parent = curr;
if(t->data > curr->data) curr = curr->right;
else curr = curr->left;
}
if(t->data < parent->data)
parent->left = t;
else
parent->right = t;
}
}
void inorder(tree_node* p)
{
if(p!=NULL)
{
if(p->left)
inorder(p->left);
cout<<" "<
if(p->right)
inorder(p->right);
}
else
return;
}
void print_inorder()
{ inorder(root); }
void preorder(tree_node* p)
{
if(p!=NULL)
{
if(p->left)
preorder(p->left);
cout<<" "<
if(p->right)
preorder(p->right);
}
else
return;
}
void print_preorder()
{ preorder(root); }
void postorder(tree_node* p)
{
if(p!=NULL)
{
if(p->left)
postorder(p->left);
cout<<" "<
if(p->right)
postorder(p->right);
}
else
return;
}
void print_postorder()
{ postorder(root); }
int main()
{
root=NULL;
int ch,tmp;
while(1)
{
system("cls");
cout<<"\n Menu Utama Operasi Pohon Biner";
cout<<" -----------------------------";
cout<<" 1. Insert/Tambah Data";
cout<<" 2. Kunjungan In-Order";
cout<<" 3. Kunjungan Pre-Order";
cout<<" 4. Kunjungan Post-Order";
cout<<" 5. Exit";
cout<<" Pilihan Anda : ";cin>>ch;
switch(ch)
{
case 1 :
cout<<" Masukan Data : ";cin>>tmp;
insert(tmp);
break;
case 2 :
cout<<" Kunjungan In-Order";
cout<<" ------------------";
print_inorder();getch();
break;
case 3 :
cout<<" Kunjungan Pre-Order";
cout<<" -------------------";
print_preorder();getch();
break;
case 4 :
cout<<" Kunjungan Post-Order";
cout<<" --------------------";
print_postorder();getch();
break;
case 5 : return 0;
break;
default:
cout<<" Pilihan yang Anda Masukkan salah!";
getch();
break;
}
}
}
http://www.mediafire.com/?7iy971zw1hsec55 atau http://www.mediafire.com/?94ok2ts2l8m64ro
Maaf Link ini dicopas saja yaa buka di Tab baru :)