二叉树的创建及其遍历
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #include <stdio.h> #include <stdlib.h> struct Bintree { char info; struct Bintree *lchild,*rchild; }; typedef struct Bintree *BinTreeNode; typedef struct Node *PNode; //栈的存储结构 struct Node { BinTreeNode c; PNode link; }; struct LinkStack //链接栈类型定义 { PNode top; //指向栈顶元素 }; typedef struct LinkStack *PLStack; //链接栈类型定的指针类型 PLStack Cstack() //创建空栈 { PLStack plstack; plstack=(PLStack)malloc( sizeof (PLStack)); if (plstack!=NULL) plstack->top=NULL; else printf( "out of space" ) ; return plstack; } int isemptystack(PLStack plstack) //判断是否是空栈 { return (plstack->top==NULL); } void push(PLStack plstack, BinTreeNode x) //进栈运算 { PNode p; p=(PNode)malloc( sizeof ( struct Node)); if (p==NULL) printf( "out of space" ); else { p->c=x; p->link=plstack->top; plstack->top=p; } } void pop (PLStack plstack) //出栈运算 { PNode p; if (isemptystack(plstack)) //判断栈是否为空,空栈不进行任何操作, //否则出栈运算,修改指针 printf( "Empty stack pop.\n" ); else { p=plstack->top; printf( "%c" ,p->c->info); plstack->top=plstack->top->link; free(p); } } BinTreeNode top(PLStack plstack) //取栈顶元素 { if (plstack->top==NULL) //判断栈顶是否空栈 printf( "Stack is Empty" ); else { return (plstack->top->c); } } BinTreeNode CBintree() { char c; BinTreeNode p; scanf( "%c" ,&c); getchar(); if (c!= '#' ) { p=(BinTreeNode)malloc( sizeof (BinTreeNode)); p->info=c; p->lchild=CBintree(); p->rchild=CBintree(); } else p=NULL; return p; } void npreOreder(BinTreeNode t) //先根次序次序周游 { PLStack s; BinTreeNode c; if (t==NULL) return ; s=Cstack(); push(s,t); while (!isemptystack(s)) { c=top(s);pop(s); if (c!=NULL) { if (c->rchild!=NULL) push(s,c->rchild); if (c->lchild!=NULL) push(s,c->lchild); } } } void ninOder(BinTreeNode t) //中根次序周游 { PLStack s=Cstack(); BinTreeNode c=t; if (c==NULL) return ; do { while (c!=NULL){push(s,c);c=c->lchild; } c=top(s);pop(s);c=c->rchild; } while (c!=NULL||!isemptystack(s)); } void npostOrder(BinTreeNode t) { PLStack s=Cstack(); BinTreeNode p=t; while (p!=NULL||!isemptystack(s)) { while (p!=NULL){ push(s,p) ; p=p->lchild?p->lchild:p->rchild; } p=top(s); pop(s); if (!isemptystack(s)&&top(s)->lchild==p) p=top(s)->rchild; else p=NULL; } } int dfs_Tree(BinTreeNode t, int a){ if (t==NULL){ return a; } if (t->lchild!=NULL) return dfs_Tree(t->lchild,a+1); if (t->rchild!=NULL) return dfs_Tree(t->rchild,a+1); return a; } int main() { BinTreeNode root=(BinTreeNode)malloc( sizeof (BinTreeNode)); root=CBintree(); printf( "先根" ); npreOreder(root); printf( "中根" ); ninOder(root); printf( "后根" ); npostOrder(root); printf( "\n%d" ,dfs_Tree(root,1)); return 0; } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步