#include <bits/stdc++.h>
#include "Btree.cpp"
using namespace std;
BTNode *Search(BTNode *bt, int x){
if(bt == NULL)
return NULL;
if(bt->data == x)
return bt;
if(bt->data > x)
Search(bt->lchild, x);
else
Search(bt->rchild, x);
}
int main(){
BTNode *bt;
int a[]={4,3,2,8,6,7,9};
int b[]={2,3,4,6,7,8,9};
int n=sizeof(a)/sizeof(a[0]);
bt=CreateBTree(a,b,n);
DispBTree(bt);
cout << endl;
int x = 6;
BTNode *node = Search(bt, x);
if(node != NULL)
cout << "找到节点:" << node->data;
else
cout << "没有找到节点!";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef struct BNode {
int data;
struct BNode *lchild,*rchild;
} BTNode;
BTNode *CreateBTree(int a[],int b[], int n) {
int k;
if(n <= 0)
return NULL;
BTNode *bt =(BTNode *) malloc (sizeof(BTNode));
bt -> data = a[0];
for(k = 0; k < n; k++)
if(a[0] == b[k])
break;
bt -> lchild = CreateBTree(a+1, b, k);
bt -> rchild = CreateBTree(a+k+1, b+k+1,n-1-k);
return bt;
}
void DispBTree(BTNode *bt) {
if (bt != NULL) {
cout << bt->data << " ";
DispBTree(bt->lchild);
DispBTree(bt->rchild);
}
}
void DestroyBTree(BTNode *bt) {
if(bt != NULL) {
DestroyBTree(bt->lchild);
DestroyBTree(bt->rchild);
free(bt);
}
}