二叉树的建树,层次建树,深度优先遍历,广度优先遍历
二叉树前序、中序、后序遍历
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
| #include <stdio.h> #include <stdlib.h>
typedef char BiElemType; typedef struct BiTNode { BiElemType c; struct BiTNode *lchild; struct BiTNode *rchild; } BiTNode, *BiTree;
typedef struct tag { BiTree p; struct tag *pnext; } tag_t, *ptag_t;
void preOrder(BiTree p) { if (NULL != p) { printf("%c", p->c); preOrder(p->lchild); preOrder(p->rchild); } }
void inOrder(BiTree p) { if (NULL != p) { inOrder(p->lchild); printf("%c", p->c); inOrder(p->rchild); } }
void posOrder(BiTree p) { if (NULL != p) { posOrder(p->lchild); posOrder(p->rchild); printf("%c", p->c); } }
typedef BiTree ElemType; typedef struct LinkNode { ElemType data; struct LinkNode *next; } LinkNode;
typedef struct { LinkNode *front, *rear; } LinkQueue;
void initQueue(LinkQueue &Q) { Q.front = Q.rear = (LinkNode *) malloc(sizeof(LinkNode)); Q.front->next = NULL; }
bool isEmptyQueue(LinkQueue Q) { return Q.front == Q.rear; }
void enQueue(LinkQueue &Q, ElemType e) { LinkNode *p = (LinkNode *) malloc(sizeof(LinkNode)); p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; }
bool deQueue(LinkQueue &Q, ElemType &e) { if (Q.front == Q.rear) { return false; } LinkNode *p = Q.front->next; e = p->data; Q.front->next = p->next; if (Q.rear == p) { Q.rear = Q.front; } free(p); return true; }
void levelOrder(BiTree T) { LinkQueue Q; initQueue(Q); BiTree p; enQueue(Q, T); while (!isEmptyQueue(Q)) { deQueue(Q, p); putchar(p->c); if (p->lchild != NULL) { enQueue(Q, p->lchild); } if (p->rchild != NULL) { enQueue(Q, p->rchild); } } }
int main() { BiTree pnew; BiTree tree = NULL; char c; ptag_t phead = NULL, ptail = NULL, listpnew = NULL, pcur; while (scanf("%c", &c)) { if (c == '\n') { break; } pnew = (BiTree) calloc(1, sizeof(BiTNode)); pnew->c = c; listpnew = (ptag_t) calloc(1, sizeof(tag_t)); listpnew->p = pnew; if (NULL == tree) { tree = pnew; phead = listpnew; ptail = listpnew; pcur = listpnew; } else { ptail->pnext = listpnew; ptail = listpnew; if (NULL == pcur->p->lchild) { pcur->p->lchild = pnew; } else if (NULL == pcur->p->rchild) { pcur->p->rchild = pnew; pcur = pcur->pnext; } } }
printf("--------------preOrder()----------------\n"); preOrder(tree); printf("\n"); printf("--------------inOrder()-----------------\n"); inOrder(tree); printf("\n"); printf("--------------posOrder()-----------------\n"); posOrder(tree);
printf("\n"); printf("--------------levelOrder()-----------------\n"); levelOrder(tree); printf("\n"); return 0; }
|