#include "Btree.cpp"
#include <bits/stdc++.h>
int Nodenum(BTNode *bt,int k ) {
if(bt == NULL)
return 0;
int lnum = Nodenum(bt->lchild,k);
int rnum = Nodenum(bt->rchild,k);
if(bt->data > k)
return lnum + rnum + 1;
else
return lnum + rnum;
}
int main() {
BTNode *bt;
int num;
int a[]= {5,2,3,4,1,6};
int b[]= {2,3,5,1,4,6};
int n = sizeof(a) / sizeof(a[0]);
bt = CreateBTree(a,b,n);
DispBTree(bt);
cout << endl;
cout << "二叉树 bt 中所有结点值大于等于 3 的结点个数为:" << Nodenum(bt,3);
DestroyBTree(bt); // 释放结点
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) {
return ;
}else{
DestroyBTree(bt -> lchild);
DestroyBTree(bt -> rchild);
free(bt);
}
}