#include <cstdlib>
#include <iostream>
using namespace std;
struct node {
int item;
node *left;
node *right;
};
class BinaryTree{
public :
BinaryTree();
~BinaryTree();
void destroy_tree(node *leaf);
void insert(int data);
node *search(int data);
void printInOrder();
void printPreOrder();
void printPostOrder();
private :
void insert(int data, node *leaf);
node *search(int key, node *leaf);
void inOrder(node *root);
void preOrder(node *root);
void postOrder(node *root);
node *root;
};
BinaryTree::BinaryTree(){
root = NULL;
}
BinaryTree::~BinaryTree(){
destroy_tree(root);
}
void BinaryTree::destroy_tree(node *leaf)
{
if(leaf!=NULL)
{
destroy_tree(leaf->left);
destroy_tree(leaf->right);
delete leaf;
}
}
void BinaryTree::insert(int data, node *leaf){
if(data< leaf->item)
{
if(leaf->left!=NULL)
insert(data, leaf->left);
else
{
leaf->left=new node;
leaf->left->item=data;
leaf->left->left=NULL;
leaf->left->right=NULL;
}
}
else if(data>=leaf->item)
{
if(leaf->right!=NULL)
insert(data, leaf->right);
else
{
leaf->right=new node;
leaf->right->item=data;
leaf->right->left=NULL;
leaf->right->right=NULL;
}
}
}
node *BinaryTree::search(int data, node *leaf)
{
if(leaf!=NULL)
{
if(data==leaf->item)
return leaf;
if(data<leaf->item)
return search(data, leaf->left);
else
return search(data, leaf->right);
}
else return NULL;
}
void BinaryTree::insert(int data){
if(root!=NULL)
insert(data, root);
else
{
root=new node;
root->item=data;
root->left=NULL;
root->right=NULL;
}
}
node *BinaryTree::search(int data)
{
//return search(data, root);
}
void BinaryTree::inOrder(node *root){
if(root!=NULL){
inOrder(root->left);
cout << root->item << " ";
inOrder(root->right);
}
}
void BinaryTree::printInOrder(){
inOrder(root);
}
void BinaryTree::preOrder(node *root){
if(root!=NULL){
cout << root->item << " ";
preOrder(root->left);
preOrder(root->right);
}
}
void BinaryTree::printPreOrder(){
preOrder(root);
}
void BinaryTree::postOrder(node *root){
if(root!=NULL){
postOrder(root->left);
postOrder(root->right);
cout << root->item << " ";
}
}
void BinaryTree::printPostOrder(){
postOrder(root);
}
int main()
{
BinaryTree a ;
a.insert(65);
a.insert(5);
a.insert(70);
a.insert(3);
a.insert(10);
a.insert(66);
cout << "INORDER " << " : ";
a.printInOrder();
cout << endl;
cout << "PREORDER " << " : ";
a.printPreOrder();
cout << endl << "POST ORDER " << " : ";
a.printPostOrder();
system("PAUSE");
return 0;
}
Sabtu, 01 Januari 2011
Langganan:
Posting Komentar (Atom)
Entri Populer
-
Zulkarnain (09018289) Tugas Sdata Array Dinamis menggunakan Stack,. ada dua operasi dalam Stack yaitu : - Push - Pop ...
-
#include <iostream.h> template <class T> class Array1D{ friend ostream& operator<<(ostream&,const Array1...
-
#include <cstdlib> #include <iostream> using namespace std; class Node{ friend class List; friend ostream&...
-
#include <iostream.h> class Bilangan{ friend ostream& operator<<(ostream&,const Bilangan&); friend is...
-
#include <cstdlib> #include <iostream> using namespace std; struct node { int item; node *left; node *right; }; class...
-
#include <cstdlib> #include <iostream> using namespace std; template<class T> class Kompleks{ friend class Op...
-
#include <cstdlib> #include <iostream.h> #define maks 5 class Stack{ friend ostream& operator<<(ostream...
-
#include <cstdlib> #include <iostream> #define maks5 using namespace std; class Array1D{ friend ostream& opera...
-
#include <cstdlib> #include <iostream> #define maks 5 using namespace std; class ArrayID{ friend ostream& operator<...
-
#include <cstdlib> #include <iostream> #define maks 5 using namespace std; class Que{ friend ostream& operator...
Tidak ada komentar:
Posting Komentar