本篇文章中所有数据结构都是后期整理的,如有问题欢迎指正,转载请注明出处http://www.cnblogs.com/a1982467767/p/8893567.html
二叉树操作设计和实现
实验目的:
掌握二叉树的定义、性质及存储方式,各种遍历算法。
实验要求:
采用二叉树链表作为存储结构,完成二叉树的建立,先序、中序和后序以及按层次遍历的操作,求所有叶子及结点总数的操作。
1 #include <stdio.h> 2 #include <stdlib.h> 3 typedef char DataType; 4 typedef struct node{ 5 DataType data; 6 struct node *lchild,*rchild; 7 }Bnode, *BTree; 8 int count; 9 void CreateBTree(BTree *T); 10 void PreorderN(BTree T); 11 void MiddleN(BTree T); 12 void LatterN(BTree T); 13 14 #define StackSize 100 /*假定预分配的栈空间最多为10*/ 15 typedef BTree SDataType; /*栈的元素类型设为整型*/ 16 #define Error printf 17 18 typedef struct{ 19 SDataType data[StackSize]; 20 int top; 21 }SeqStack; 22 23 SeqStack * InitStack(void) /*初始栈*/ 24 { 25 SeqStack *S = (SeqStack *)malloc(sizeof(SeqStack) * StackSize); 26 S->top=-1; 27 } 28 29 int StackEmpty(SeqStack *S) /*判栈空*/ 30 { 31 return S->top==-1; 32 } 33 34 int StackFull(SeqStack *S) /*判栈满*/ 35 { 36 return S->top==StackSize-1; 37 } 38 39 void Push(SeqStack *S, SDataType x) /*进栈*/ 40 { 41 if(StackFull(S)) 42 Error("STACK FULL! "); /*上溢退出*/ 43 else 44 S->data[++S->top]=x; /*栈顶指针加1后将x进栈*/ 45 } 46 47 SDataType Pop(SeqStack *S) /*出栈*/ 48 { 49 if (StackEmpty(S)) 50 Error("Stack underflow"); /*下溢退出*/ 51 else 52 return S->data[S->top--]; /*栈顶指针返回后将栈顶指针减1*/ 53 } 54 55 SDataType StackTop(SeqStack *S) /*取栈顶元素*/ 56 { 57 if (StackEmpty(S)) 58 Error("STACK EMPTY! "); 59 return S->data[S->top]; 60 } 61 62 main() 63 { 64 BTree T; 65 char ch1,ch2; 66 printf(" PLEASE SELECT: "); 67 ch1='y'; 68 while(ch1=='y' || ch1=='Y') 69 { 70 printf(" A------------------------CREATE BITREE"); 71 printf(" B-------------------------DLR(NO DI IGU)"); 72 printf(" C----------------------Middle(NO DI IGU)"); 73 printf(" D----------------------Latter(NO DI IGU)"); 74 printf(" E-------------------------EXIT "); 75 scanf(" %c",&ch2); 76 switch(ch2) 77 { 78 case 'A': 79 case 'a':printf("INPUT NODE: "); 80 CreateBTree(&T); 81 printf("CREATE SUCC ");break; 82 case 'B': 83 case 'b':printf("BIAN LI JIE GUO "); 84 PreorderN(T);break; 85 case 'C': 86 case 'c':printf("Middle Bian Li Jie Guo "); 87 MiddleN(T);break; 88 case 'D': 89 case 'd':printf("Latter Bian Li Jie Guo "); 90 LatterN(T);break; 91 case 'E': 92 case 'e':ch1='n';break; 93 default:ch1='n'; 94 } 95 } 96 } 97 void CreateBTree(BTree *T) 98 { 99 char ch; 100 scanf(" %c",&ch); 101 if (ch=='0') 102 *T=NULL; 103 else 104 { 105 *T=(Bnode*)malloc(sizeof(Bnode)); 106 (*T)->data=ch; 107 CreateBTree(&(*T)->lchild); 108 CreateBTree(&(*T)->rchild); 109 } 110 } 111 void PreorderN(BTree T) 112 {/*先序遍历二叉树T的非递归算法*/ 113 SeqStack *S; 114 BTree p; 115 p = T; 116 S = InitStack(); 117 while(!StackEmpty(S) || p != NULL) 118 { 119 if(p) 120 { 121 printf("%3c",p->data); /*访问入栈结点的数据域*/ 122 Push(S,p); /*向左走到尽头*/ 123 p = p->lchild; 124 } 125 else 126 { 127 p = Pop(S); 128 p = p->rchild; 129 } 130 } 131 }/*PreorderN */ 132 133 void MiddleN(BTree T) 134 { 135 SeqStack *S; 136 BTree p; 137 p = T; 138 S = InitStack(); 139 while(!StackEmpty(S) || p != NULL) 140 { 141 if(p) 142 { 143 /*向左走到尽头*/ 144 Push(S,p); 145 p = p->lchild; 146 } 147 else 148 { 149 p = Pop(S); 150 printf("%3c",p->data);/*访问入栈结点的数据域*/ 151 p = p->rchild; 152 } 153 } 154 } 155 156 void LatterN(BTree T) 157 { 158 SeqStack *S1,*S2; 159 BTree p; 160 p = T; 161 S1 = InitStack(); 162 S2 = InitStack(); 163 while(!StackEmpty(S1) || p != NULL) 164 { 165 if(p) 166 { 167 /*向左走到尽头*/ 168 Push(S1,p); 169 Push(S2,p); 170 p = p->rchild; 171 } 172 else 173 { 174 p = Pop(S1); 175 p = p->lchild; 176 } 177 } 178 while(!StackEmpty(S2)) 179 { 180 p = Pop(S2); 181 printf("%3c",p->data);/*访问入栈结点的数据域*/ 182 } 183 }