數據結構設計——二叉樹實現
阿新 • • 發佈:2018-04-20
AS stdlib.h .com hle 要求 類型 html 層次 logs
本篇文章中所有數據結構都是後期整理的,如有問題歡迎指正,轉載請註明出處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 #defineError 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!\n"); /*上溢退出*/ 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!\n"); 59 return S->data[S->top]; 60 } 61 62 main() 63 { 64 BTree T; 65 char ch1,ch2; 66 printf("\nPLEASE SELECT:\n"); 67 ch1=‘y‘; 68 while(ch1==‘y‘ || ch1==‘Y‘) 69 { 70 printf("\nA------------------------CREATE BITREE"); 71 printf("\nB-------------------------DLR(NO DI IGU)"); 72 printf("\nC----------------------Middle(NO DI IGU)"); 73 printf("\nD----------------------Latter(NO DI IGU)"); 74 printf("\nE-------------------------EXIT\n"); 75 scanf("\n%c",&ch2); 76 switch(ch2) 77 { 78 case ‘A‘: 79 case ‘a‘:printf("INPUT NODE:\n"); 80 CreateBTree(&T); 81 printf("CREATE SUCC\n");break; 82 case ‘B‘: 83 case ‘b‘:printf("BIAN LI JIE GUO\n"); 84 PreorderN(T);break; 85 case ‘C‘: 86 case ‘c‘:printf("Middle Bian Li Jie Guo\n"); 87 MiddleN(T);break; 88 case ‘D‘: 89 case ‘d‘:printf("Latter Bian Li Jie Guo\n"); 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("\n%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 }
數據結構設計——二叉樹實現