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 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include <stdio.h> #include <stdlib.h>
#define MaxSize 100 typedef char ElemType;
typedef struct node { ElemType data; struct node *lchild; struct node *rchild; } BTNode;
BTNode *CreateBTNode(char *str); BTNode *FindNode(BTNode *b, ElemType x); BTNode *LchildNode(BTNode *p); BTNode *RchildNode(BTNode *p); int BTNodeDepth(BTNode *b); void DispBTNode(BTNode *b); void DestroyBTNode(BTNode *b);
int main() { BTNode *b, *p, *lp, *rp; printf(" (1)创建二叉树:"); b = CreateBTNode("A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); printf("\n"); printf(" (2)输出二叉树:"); DispBTNode(b); printf("\n"); printf(" (3)查找H节点:"); p = FindNode(b, 'H'); if (p != NULL) { lp = LchildNode(p); if (lp != NULL) printf("左孩子为%c ", lp->data); else printf("无左孩子 "); rp = RchildNode(p); if (rp != NULL) printf("右孩子为%c", rp->data); else printf("无右孩子 "); } else printf(" 未找到!"); printf("\n"); printf(" (4)二叉树b的深度:%d\n", BTNodeDepth(b)); printf(" (5)释放二叉树b\n"); DestroyBTNode(b); return 0; }
void DestroyBTNode(BTNode *b) { if (b != NULL) { DestroyBTNode(b->lchild); DestroyBTNode(b->rchild); free(b); } }
int BTNodeDepth(BTNode *b) { int lchilddep, rchilddep; if (b == NULL) return 0; else { lchilddep = BTNodeDepth(b->lchild); rchilddep = BTNodeDepth(b->rchild); return (lchilddep > rchilddep) ? (lchilddep + 1) : (rchilddep + 1); } }
BTNode *LchildNode(BTNode *p) { if (p == NULL) return NULL; return p->lchild; }
BTNode *RchildNode(BTNode *p) { if (p == NULL) return NULL; return p->rchild; }
BTNode *CreateBTNode(char *str) { BTNode *st[MaxSize], *p = NULL,*b = NULL; int top = -1, k, j = 0; char ch = str[j]; while (ch != '\0') { switch (ch) { case '(': top++; st[top] = p; k = 1; break; case ')': top--; break; case ',': k = 2; break; default: p = (BTNode *)malloc(sizeof(BTNode)); p->data = ch; p->lchild = p->rchild = NULL; if (b == NULL) b = p; else { switch (k) { case 1: st[top]->lchild = p; break; case 2: st[top]->rchild = p; break; } } break; } ch = str[++j]; }
return b; }
void DispBTNode(BTNode *b) { if(b != NULL) { printf("%c",b->data); if(b->lchild != NULL || b->rchild != NULL) { printf("("); if(b->lchild != NULL) DispBTNode(b->lchild); if(b->rchild != NULL) { printf(","); DispBTNode(b->rchild); } printf(")"); } } }
BTNode *FindNode(BTNode *b, ElemType x) { BTNode *p; if (b == NULL) return NULL; else if (b->data == x) return b; else { p = FindNode(b->lchild, x); if (p != NULL) return p; else return FindNode(b->rchild, x); } }
|