//二叉树 //储存结构:顺序表 #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define MAX_TREE_SIZE 100 //二叉树的最大节点数 #define Clear Init //顺序存储中两函数完全一样,下同 #define Destroy Init //typedef int TElemType; typedef char TElemType; typedef TElemType SqBiTREE[MAX_TREE_SIZE]; TElemType Nil; //节点为空标志 void Init(SqBiTREE &T) { for(int i = 0; i < MAX_TREE_SIZE; i++){ T[i] = Nil; } } bool Empty(SqBiTREE T) { if(T[1] == Nil) return true; else return false; } //按层序次序输入二叉树中节点的值,n为节点个数,构造顺序存储的二叉树T void Create(SqBiTREE &T,int n) { char c; int i = 1,j = 0; Init(T); T[0] = n; //0位置储存节点个数 while(true) { //scanf("%c",&c); 吃回车? cin>>c; if(c != Nil){ T[i] = c; j++; } i++; if(n == j) break; //储存结束 } } //判断空二叉树,若二叉树为空,返回true bool Empty(SqBiTREE T) { if(T[0] == 0) return true; else return false; } //公式:K =「log2n」+1 int Depth(SqBiTREE T) { int i,k; for(i = MAX_TREE_SIZE - 1; i > 0; i--){ if(T[i] != Nil) break; } k = log(i)/log(2) + 1; return k; } void PreTraverse(SqBiTREE T,int root) { printf("%c ",T[root]); if(T[2 * root] != Nil) PreTraverse(T,2 * root); if(T[2 * root + 1] != Nil) PreTraverse(T,2 * root + 1); } void InTraverse(SqBiTREE T,int root) { if(T[2 * root] != Nil) PreTraverse(T,2 * root); printf("%c ",T[root]); if(T[2 * root + 1] != Nil) PreTraverse(T,2 * root + 1); } void PostTraverse(SqBiTREE T,int root) { if(T[2 * root] != Nil) PreTraverse(T,2 * root); if(T[2 * root + 1] != Nil) PreTraverse(T,2 * root + 1); printf("%c ",T[root]); } //打印 void Prt(SqBiTREE T) { for(int i = 0; i <= 10; i++){ printf("%c ",T[i]); } printf("\n"); } int main() { int n; Nil = ‘0‘; //空节点标志 SqBiTREE t; while(scanf("%d",&n) != EOF){ Create(t,3); printf("%d\n",Depth(t)); } return 0; }
//二叉树 //储存方式:链式 typedef int Status; typedef char TElemType[12]; typedef struct BiTNode { TElemType data; BiTNode *lchild,*rchild; }BiTNode,*BitTree; void Init(BitTree &T){ T = NULL; } void Destroy(BitTree &T){ if(T) { if(T->lchild){ Destroy(T->lchild); } if(T->rchild){ Destroy(T->rchild); } free(T); T = NULL; } } Status Empty(BitTree T){ if(T) return FALSE; else return TRUE; } void Create(BitTree &T){ //0为空 TElemType ch; scanf("%s",ch); if(strcmp(ch,"0") == 0){ T = NULL; } else{ T = (BitTree)malloc(sizeof(BiTNode));//生成根节点 if(!T){ exit(OVERFLOW); } strcpy(T->data,ch); Create(T->lchild); Create(T->rchild); } } int depth(BitTree t){ //t 的深度为其左右子树的深度中的大者+1 int i,j; if(!t){ return 0; }//树的深度为0 if(t->lchild){ i = depth(t->lchild); } else{ i = 0; } if(t->rchild){ j = depth(t->rchild); } else{ j = 0; } return i > j ? i + 1: j + 1; } void PreOrderTraverse(BitTree T){ if(T){ printf("%s ",T->data); PreOrderTraverse(T->lchild); PreOrderTraverse(T->rchild); } } void InOrderTraverse(BitTree T){ if(T){ InOrderTraverse(T->lchild); printf("%s ",T->data); InOrderTraverse(T->rchild); } } void PostOrderTraverse(BitTree T){ if(T){ PostOrderTraverse(T->lchild); PostOrderTraverse(T->rchild); printf("%s ",T->data); } } int main() { BitTree T; Init(T); Create(T); printf("先序遍历:"); PreOrderTraverse(T); printf("\n"); printf("中序遍历:"); InOrderTraverse(T); printf("\n"); printf("后续遍历:"); PostOrderTraverse(T); printf("\n"); return 0; }
原文:http://www.cnblogs.com/chenyang920/p/5002505.html