当初写个论坛分页,对二叉树感到很新奇,简单分页的话都不难,可谓一种神奇的结构,事实上搜索引擎也是大的树吧。
其中水平的顺序输入树后,找到相对应的节点的位置的算法相当微妙。二叉树中0代表空,按-1结束输入。可以进行中序,先序和后序输出。
还是分为 tree.h ,tree .c , main.c
tree.h
#include<stdio.h>
#include<malloc.h>
#define MAX 100
typedef int DataType;
typedef struct BinTree *PNode;
struct BinTree{
DataType info;
PNode llink;
PNode rlink;
};
typedef struct BinTree *PTree;
PTree createTree();
void levelorder(PTree ptree);
int isEmptyTree(PTree ptree);
PTree initTree(PTree ptree,DataType *data,int number);
PNode rootOfTree(PTree ptree);
PNode llinkTree(PTree ptree);
PNode rlinkTree(PTree ptree);
void visit(PNode pnode);
PNode parentTree(PTree ptree,PNode pnode);
void inorder(PTree ptree);
void preorder(PTree ptree);
void backorder(PTree ptree);
int depth(PTree ptree);
int leafNumber(PTree ptree);
tree .c
#include "tree.h"
int main(){
int data[100];
int n = 0;
PTree ptree = createTree();
printf("Please input the data of tree in level order(ending with -1:
");
while(1){
scanf("%d",&data[n]);
if(data[n] == -1) break;
n++;
}
//printf("%d",n);
// 自己发毛了 init只是return tree 我竟然没有把原始的ptree 赋值进去!
ptree = initTree(ptree,data,n);
printf("the depth is %d
",depth(ptree));
printf(" the leafnumber is %d
",leafNumber(ptree));
// levelorder(ptree);
//visit(ptree);
printf("
");
printf("the level order is :
");
levelorder(ptree);
printf("the in order is :
");
inorder(ptree);
}
main.c
#include "tree.h"
#include<malloc.h>
PTree createTree(){
PTree ptree = (PTree)malloc(sizeof(struct BinTree));
if(ptree == NULL) printf("create fail");
ptree ->llink = NULL;
ptree ->rlink = NULL;
return ptree;
}
void levelorder(PTree ptree){
int i,j;
PNode pnode[MAX] = {NULL};
pnode[0] = ptree;
i = 0; j = 1;
while(j>i){
PNode temp = pnode[i];
if(temp != NULL){
if(temp ->llink != NULL) pnode[j++] = temp ->llink;
if(temp ->rlink != NULL) pnode[j++] = temp ->rlink;
printf("%d
",temp->info);
}
i++;
}
printf("
");
}
int isEmptyTree(PTree ptree){
return (ptree ->llink==NULL&&ptree ->rlink==NULL);
}
PTree initTree(PTree ptree,DataType *data,int number){
int i;
PNode temp;
PNode pnode[MAX] ={NULL};
for(i = 0; i<number ; i++){
//the 0 represent the node NULL
if(data[i] != 0)
// 尝试不分配空间 直接用数组的pnode 直接赋值
//不行 只是数组而已 里面的空间没有分配出来
temp = (PNode)malloc(sizeof(struct BinTree));
temp ->llink = NULL;
temp ->rlink = NULL;
temp ->info = data[i];
pnode[i] = temp;
// printf("%d
",pnode[i]->info);
}
for(i = 0; i<number ; i++){
if(pnode[i] != NULL && i*2+1 < MAX)
pnode[i] ->llink = pnode[i*2+1];
}
for(i = 0; i<number ; i++){
if(pnode[i] != NULL && i*2+2 < MAX)
pnode[i] ->rlink = pnode[i*2+2];
}
ptree = pnode[0];
return ptree;
}
PNode rootOfTree(PTree ptree){
PNode p ;
if(ptree == NULL) printf("the root does not exist");
p = ptree;
return p;
}
PNode llinkTree(PTree ptree){
if(ptree == NULL) printf("the llink does not exist");
return NULL;
return ptree->llink;
}
PNode rlinkTree(PTree ptree){
if(ptree == NULL) printf("the rlink does not exist");
return ptree->rlink;
}
void visit(PNode pnode){
if(pnode == NULL) printf("it is null");
printf("%d",pnode->info);
}
PNode parentTree(PTree ptree,PNode pnode){
PNode temp = NULL;
if(ptree == NULL){
printf("it is null");
return NULL;
}
else if(pnode = ptree){
printf("it is null");
return NULL;
}
else if(ptree ->llink == pnode || ptree ->rlink == pnode)
return ptree;
if(ptree != NULL && temp == NULL) temp = parentTree(ptree->llink,pnode);
if(ptree != NULL && temp == NULL) temp = parentTree(ptree->rlink,pnode);
return temp;
}
void inorder(PTree ptree){
if(ptree == NULL) return;
inorder(ptree ->llink);
visit(rootOfTree(ptree));
inorder(ptree ->rlink);
}
void preorder(PTree ptree){
if(ptree == NULL) return;
visit(rootOfTree(ptree));
preorder(ptree ->llink);
preorder(ptree ->rlink);
}
void backorder(PTree ptree){
if(ptree == NULL) return;
backorder(ptree ->llink);
backorder(ptree ->rlink);
visit(rootOfTree(ptree));
}
int depth(PTree ptree){
int i = 0,j = 0;
// if(ptree != NULL )
// return 1;
if(ptree == NULL) return 0;
if(ptree != NULL){
i = depth(ptree ->llink);
j = depth(ptree ->rlink);
}
return i>j?i+1:j+1;
}
int leafNumber(PTree ptree){
int i = 0;
if(ptree != NULL && ptree ->llink == NULL && ptree ->rlink == NULL){
// printf("%d",ptree ->info);
return 1;
}
if(ptree == NULL) return 0;
if(ptree != NULL){
i = leafNumber(ptree ->llink)+leafNumber(ptree ->rlink);
}
return i;
}
这些基础的东西,看到了指针的用处,真的很灵活,自由度高,对于一些人来讲,确实是王道。