#include "binarytree.h"
void BinaryTree::print(Node_Ptr x){
if(x!=NULL){
print(x->left);
cout<<x->key<<"";
print(x->right);
}
}
Node_Ptr BinaryTree::treeSearch(Node_Ptr t, int x){
Node_Ptr searchData=(Node_Ptr)new Node;
if((t == NULL)|| (t->key == x))
return t;
if(x< t->key)
return treeSearch(t->left, x);
treeSearch(t->right, x);
}
Node_Ptr BinaryTree::treeInsert(Node_Ptr t, int x){
Node_Ptr r;
r=(Node_Ptr)new Node;
if(t==NULL){
r->key=x;
r->left=NULL;
r->right=NULL;
return r;
}
if(x<t->key)
{t->left=treeInsert((t->left),x);
return t;}
else
{t->right=treeInsert((t->right),x);
return t;}
}
void BinaryTree::insertData(int *input, int n){
r->key=x;
r->left=NULL;
r->right=NULL;
if(t==NULL)
root=t;
else{
Node_Ptr p=NULL;
tmp=t;
while(tmp != NULL){
p=tmp;
if(x<tmp->key)
tmp->left[tmp];
else
tmp->right[tmp];
}
if(x<p->key)
p->left=r;
else
p->right=r;
}
void BinaryTree::getData(int *input, int n){
for(int i=1;i<n+1;i++){
input[i]=rand()%(2*n);
}
}
---------------binarytree.cpp
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
using namespace std;
typedef class Node *Node_Ptr;
class Node {
public:
int key;
Node_Ptr left;
Node_Ptr right;
Node_Ptr parent;
};
class BinaryTree{
private:
Node_Ptr root;
public:
void getData(int *input, int n);
void insertData(int *input, int n);
void searchData(int x);
void print(Node_Ptr x);
Node_Ptr treeInsert(Node_Ptr t, int x);
Node_Ptr treeSearch(Node_Ptr t, int x);
};
#endif
------------------binarytree.h
#include <iostream>
#include"binarytree.h"
using namespace std;
int main(){
BinaryTree b;
int *input, n, x;
cout<<"Enter N=";
cin>>n;
input = new int [n];
b.getData(input, n);
b.insertData(input, n);
cout<<"Enter integer:";
cin>>x;
b.searchData(x);
system("PAUSE");
return 0;
}
---------main.cpp
'Not Using > algorithm' 카테고리의 다른 글
DP (0) | 2009.12.01 |
---|---|
Dynamic programming (0) | 2009.11.17 |
해쉬테이블 완성 (0) | 2009.11.09 |
완성못한 해쉬테이블 (0) | 2009.11.03 |
이런망할이진검색트리 (0) | 2009.10.27 |