- 二叉排序树的构建和查找算法
- 求二叉树的宽度算法
- 求二叉树的深度算法
- 二叉树的构建算法
- *
#include <stdio.h>
#include <stdlib.h>
typedef struct BTNode{
int data;
struct BTNode * lchild;
struct BTNode * rchild;
}BTNode;
BTNode * searchTBTNode(BTNode * bt,int key){
if(bt == NULL){
return NULL;
}else{
if(bt->data == key) return bt;
else if(key < bt->data) return searchTBTNode(bt->lchild,key);
else return searchTBTNode(bt->rchild,key);
}
}
int createTBT(BTNode ** bt,int e){
if((*bt) == NULL){
(*bt) = (BTNode *)malloc(sizeof(BTNode));
(*bt)->lchild = (*bt)->rchild = NULL;
(*bt)->data = e;
return 1;
}else{
if(e == (*bt)->data){
return 0;
}else if(e < (*bt)->data){
return createTBT(&(*bt)->lchild,e);
}else{
return createTBT(&(*bt)->rchild,e);
}
}
}
BTNode * createTBTTree(int arr[],int n){
int i;
BTNode * bt;
for(i=0;i<n;i++){
createTBT(&bt,arr[i]);
}
return bt;
}
int getDeep(BTNode * root){
int ld,rd;
if(root == NULL){
return 0;
}
ld = getDeep(root->lchild);
rd = getDeep(root->rchild);
return (ld>rd?rd:rd)+1;
}
typedef struct ST{
BTNode * bt;
int no;
}ST;
#define maxsize 100
int getBoard(BTNode * root){
ST st[maxsize];
int front,rear;
front = 0; rear = 0;
BTNode * s;
int currentNum;
if(root != NULL){
++rear;
st[rear].bt = root;
st[rear].no = 1;
while(rear != front){
++ front;
currentNum = st[front].no;
s = st[front].bt;
if(s->lchild){
++rear;
st[rear].bt = s->lchild;
st[rear].no = currentNum + 1;
}else if(s->rchild){
++rear;
st[rear].bt = s->rchild;
st[rear].no = currentNum + 1;
}
}
int max=0,i,j,n;
for(i=1;i<=currentNum;i++){
n = 0;
for(j=1;j<=rear;j++){
if(st[j].no == i){
n++;
}
}
if(max<n){
max = n;
}
}
return max;
}else{
return 0;
}
}
BTNode * createBTNode(){
BTNode * bt = (BTNode *)malloc(sizeof(BTNode));
int e;
scanf("%d",&e);
if(e == 0){
bt->lchild = NULL;
bt->rchild = NULL;
return NULL;
}
bt->lchild = createBTNode();
bt->rchild = createBTNode();
return bt;
}
int main(){
return 0;
}