BINARY TREE
Nama : Haniffah Asysyarifah
NIM / Kelas :
A710170005 / A
Prodi :
Pendidikan Teknik Informatika
Mata Kuliah :
Struktur Data
Tugas : Membuat Program dengan Binary Tree
Pada Program dibawah ini merupakan Program BINARY TREE menggunakan Bahasa C++
#include
<iostream>
#include
<stdio.h>
#include
<conio.h>
#include
<stdlib.h>
using
namespace std;
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<<"
"<<p->data<<" ";
if(p->right)
inorder(p->right);
}else
return;
}
void
print_inorder()
{
inorder(root);
}
void
preorder(tree_node* p)
{
if(p!=NULL)
{cout<<"
"<<p->data<<" ";
if(p->right)
preorder(p->right);
if(p->left)
preorder(p->left);
}else
return;
}
void
print_preorder()
{
preorder(root);
}
int
height(tree_node* p)
{
if(p==NULL)return
0;
int u =
height(p->left),v = height(p->right);
if(u
> v)
return
u+1;
else
return
v+1;
}
void
cari_terbesar(tree_node* p)
{
if(p==NULL)
return;
else
if(p->right==NULL)
{
cout<<"
"<<p->data<<" ";
return;
}
else
{
cari_terbesar(p->right);
return;
}
}
int
count(tree_node* p)
{
if(p==NULL)return 0;
return count(p->left) +
count(p->right)+1;
}
int
main()
{
root = NULL;
int ch, tmp;
cout<< "Nama : Haniffah Asysyarifah"<<endl;
cout<< "NIM : A710170005"<<endl;
cout<< "Kelas : A\n"<<endl;
while(1)
{
cout<<endl;
cout<<"BINARY
TREE"<<endl;
cout<<"==========================="<<endl;
cout<<"1. Insert (Tambah)
Data"<<endl;
cout<<"2. Lihat secara
In-Order"<<endl;
cout<<"3. Lihat secara
pre-order"<<endl;
cout<<"4. Hitung jumlah
node"<<endl;
cout<<"5. Hitung tinggi
pohon"<<endl;
cout<<"6. Lihat data
terbesar"<<endl;
cout<<"7.
Exit"<<endl;
cout<<"Pilihan Anda :
"; cin>>ch;
cout<<endl;
switch(ch)
{
case 1 :
cout<<"Masukkan Data
: ";
cin>>tmp;
insert(tmp);
break;
case 2 : cout<<endl;
cout<<"Tampil secara
In-Order"<<endl;
cout<<"----------------------"<<endl;
print_inorder(); getch();
break;
case 3 : cout<<endl;
cout<<"Tampil secara
Pre-Order"<<endl;
cout<<"----------------------"<<endl;
print_preorder(); getch();
break;
case 4 : cout<<"Hitung
Jumlah Node"<<endl;
cout<<"------------------"<<endl;
cout<<"Jumlah Node =
"<<count(root);
getch();
break;
case 5 :
cout<<"Menghitung Tinggi Pohon"<<endl;
cout<<"------------------"<<endl;
cout<<"Tinggi Pohon
= "<<height(root);
getch();
break;
case 6 : cout<<"Mecari
Data Terbesar"<<endl;
cout<<"------------------"<<endl;
cout<<"Data Terbesar
Adalah = "<<endl;
cari_terbesar(root);
getch();
break;
case 7 : return 0;
break;
default:
cout<<"Pilihan salah!"<<endl;
getch;
break;
}
}
}
Gambar 2.1 program output Binary Tree
Gambar
2.2 program output Binary Tree
Gambar
2.3 program output Binary Tree
Gambar
2.4 program output Binary Tree
Gambar
2.5 program output Binary Tree
Referensi :
·
Sukirman,
dan Irma Yuliana.(2017).Modul Praktikum Struktur Data.Surakarta : Universitas
Muhammadiyah Surakarta.
·
Wantoro,
Jan dan Sukirman.(2017).Algoritma & Struktur Data dalam Bahasa
C/C++.Surakarta : Muhammadiyah University Press.
Komentar
Posting Komentar