本文最后更新于:3 个月前
本文主要总结了数据结构关于二叉树的相关知识内容,其中包括定义、过程算法和完整实现代码。
二叉树(BiTree)
定义
二叉树的遍历(traversing binary tree)是指从根结点出发,按照某种次序一次访问二叉树中的所有结点,使得每个结点被访问一次且仅被访问一次。
过程及其算法
先序遍历:根左右
1 2 3 4 5 6 7 8 9 10 void FirstOrder (BiTree T) { if (T == NULL ) { return ; } cout << T->data; FirstOrder (T->lchild); FirstOrder (T->rchild); }
中序遍历:左根右
1 2 3 4 5 6 7 8 9 10 11 12 13 14 void MidOrder (BiTree T) { if (T == NULL ) { return ; } MidOrder (T->lchild); cout << T->data; MidOrder (T->rchild); }
后序遍历:左右根
1 2 3 4 5 6 7 8 9 10 11 12 void EndOrder (BiTree T) { if (T == NULL ) { return ; } EndOrder (T->lchild); EndOrder (T->rchild); cout << T->data; }
拓展:求树的深度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 int treeDepth (BiTree T) { if (T == NULL ) { return 0 ; } else { int left = treeDepth (T->lchild); int right = treeDepth (T->rchild); if (left > right) { return left + 1 ; } else { return right + 1 ; } } }
层序遍历:(借助队列)
算法思想:
初始化一个辅助队列
根节点入队
若队列非空,则对头结点出队访问该节点,并将其左、右孩子插入队尾(如果有的话)
重复上步直至队列为空
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 void LevelOrder (BiTree T) { LinkQueue Q ; InitQueue (Q); BiTree p; p = new BiTNode; Push (Q, T); while (!IsEmpty (Q)) { Pop (Q, p); cout << p->data; if (p->lchild!=NULL ) { Push (Q,p->lchild); } if (p->rchild != NULL ) { Push (Q, p->rchild); } } }
完整实现代码
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 #include <iostream> #include <string> using namespace std;typedef struct BiTNode { char data; struct BiTNode * lchild, * rchild; }BiTNode,*BiTree;typedef struct LinkNode { BiTNode* data; struct LinkNode * next; }LinkNode;typedef struct { LinkNode* front,*rear; }LinkQueue;void createBiTree (BiTree &T) { char ch; cin >> ch; if (ch == '#' ) { T = NULL ; } else { T = new BiTNode; T->data = ch; createBiTree (T->lchild); createBiTree (T->rchild); } }void InitBiTree (BiTree& T) { T->lchild = NULL ; T->rchild = NULL ; T->data = NULL ; }void InitQueue (LinkQueue &L) { L.front = L.rear = new LinkNode; L.front->data = NULL ; L.front->next = NULL ; }int IsEmpty (LinkQueue Q) { if (Q.front == Q.rear) { return 1 ; } else { return 0 ; } }bool Push (LinkQueue& S, BiTree T) { LinkNode* p = new LinkNode; p->data = T; p->next = NULL ; S.rear->next = p; S.rear = p; return true ; }bool Pop (LinkQueue& S, BiTree& T) { if (IsEmpty (S)) { return false ; } T = S.front->next->data; LinkNode* p = S.front->next; S.front->next = p->next; if (S.rear == p) { S.rear = S.front; } delete p; return true ; }void FirstOrder (BiTree T) { if (T == NULL ) { return ; } cout << T->data; FirstOrder (T->lchild); FirstOrder (T->rchild); }void MidOrder (BiTree T) { if (T == NULL ) { return ; } MidOrder (T->lchild); cout << T->data; MidOrder (T->rchild); }void EndOrder (BiTree T) { if (T == NULL ) { return ; } EndOrder (T->lchild); EndOrder (T->rchild); cout << T->data; }void LevelOrder (BiTree T) { LinkQueue Q ; InitQueue (Q); BiTree p; p = new BiTNode; Push (Q, T); while (!IsEmpty (Q)) { Pop (Q, p); cout << p->data; if (p->lchild!=NULL ) { Push (Q,p->lchild); } if (p->rchild != NULL ) { Push (Q, p->rchild); } } }int treeDepth (BiTree T) { if (T == NULL ) { return 0 ; } else { int left = treeDepth (T->lchild); int right = treeDepth (T->rchild); if (left > right) { return left + 1 ; } else { return right + 1 ; } } }int main () { BiTree bt; createBiTree (bt); cout << "树的深度为:" << treeDepth (bt) << endl; cout << "树的先序遍历为:" << endl; FirstOrder (bt); cout << endl; cout << "树的中序遍历为:" << endl; MidOrder (bt); cout << endl; cout << "树的后序遍历为:" << endl; EndOrder (bt); cout << endl; cout<< "树的层次遍历为:" << endl; LevelOrder (bt); return 1 ; }