第七章、树和二叉树
7.3.2 二叉树链式存储
算法库:
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| #include <iostream> #include <string.h> #include <map> #include<stack> #include<queue> #include<unordered_map> using namespace std; #pragma warning(disable:4996)
typedef char ElemType; #define MaxSize 100
typedef struct node{ ElemType data; struct node* lchild; struct node* rchild; }BTNode;
void CreateBTNode(BTNode *& b,char *str) { BTNode* St[MaxSize], * p=NULL; int top = -1, k, j = 0; char ch; b = NULL; 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; } } } j++; ch = str[j]; } }
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); } }
BTNode* LchildNode(BTNode* p) { return p->lchild; }
BTNode* RchildNode(BTNode* p) { return p->rchild; }
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); } }
void DispBTNode(BTNode* b) { if (b != NULL) { printf("%c", b->data); if (b->lchild != NULL || b->rchild != NULL) { printf("("); DispBTNode(b->lchild); if (b->rchild != NULL) printf(","); DispBTNode(b->rchild); printf(")"); } } }
void DestroyBTNode(BTNode*& b) { if (b != NULL) { DestroyBTNode(b->lchild); DestroyBTNode(b->rchild); free(b); } }
void PreOrder(BTNode* b) { if (b != NULL) { printf("%c ", b->data); PreOrder(b->lchild); PreOrder(b->rchild); } }
void InOrder(BTNode* b) { if (b != NULL) { PreOrder(b->lchild); printf("%c ", b->data); PreOrder(b->rchild); } }
void PostOrder(BTNode* b) { if (b != NULL) { PreOrder(b->lchild); PreOrder(b->rchild); printf("%c ", b->data); } }
void LevelOrder(BTNode* b) { BTNode* p; queue<BTNode*> myQueue; myQueue.push(b); while (!myQueue.empty()) { p = myQueue.front(); myQueue.pop(); printf("%c ", p->data); if (p->lchild != NULL) myQueue.push(p->lchild); if (p->rchild != NULL) myQueue.push(p->rchild); } }
bool IsComplete(BTNode* b) { if (b == NULL)return true; bool leaf = false; queue<BTNode*>q; q.push(b); while (!q.empty()) { BTNode* p = q.front(); q.pop(); if ((leaf && (p->lchild != NULL || p->rchild != NULL)) || (p->lchild == NULL && p->rchild != NULL)) return false; if (p->lchild != NULL) q.push(p->lchild); if (p->rchild != NULL) q.push(p->rchild); if ((p->lchild != NULL && p->rchild == NULL) || (p->lchild == NULL && p->rchild == NULL)) leaf = true; } return true; }
void invertTree(BTNode* b) {
if (b == NULL) return;
BTNode* temp = b->lchild; b->lchild = b->rchild; b->rchild = temp;
invertTree(b->lchild); invertTree(b->rchild); }
void linkLeafNode(BTNode* p, BTNode*& head, BTNode*& tail) { if (p != NULL) { if (p->lchild == NULL && p->rchild == NULL) { if (head == NULL) { head = p; tail = p; } else { tail->rchild = p; tail = p; } } linkLeafNode(p->lchild, head, tail); linkLeafNode(p->rchild, head, tail); } }
int main() { char* str = (char*)malloc(100); printf("(1),输入二叉树的元素:\n"); scanf("%s", str);
BTNode* btnode; BTNode* head, * tail; printf("(2),创建二叉树:\n"); CreateBTNode(btnode, str); DispBTNode(btnode);
printf("\n(3),翻转二叉树:\n"); invertTree(btnode); DispBTNode(btnode); }
|
先序遍历,非递归实现,栈
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| void PreOrderl(BTNode* b) { BTNode* p; stack<BTNode*> myStack; if (b != NULL) { myStack.push(b); while (!myStack.empty()) { p = myStack.top(); myStack.pop(); printf("%c ", p->data); if (p->rchild != NULL) myStack.push(p->rchild); if (p->lchild != NULL) myStack.push(p->lchild); } printf("\n"); } }
|
三种遍历-迭代
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
| void preOrderIteration(TreeNode *root) { if (root == nullptr) return; stack<TreeNode*> stack; stack.push(root); while (!stack.empty()) { TreeNode* temp = stack.top(); stack.pop(); cout << temp->val << " "; if (temp->right != nullptr)stack.push(temp->right); if (temp->left != nullptr)stack.push(temp->left); } }
void inOrderIteration(TreeNode* root) { if (root == nullptr) { return; } TreeNode* cur = root; stack<TreeNode*> stack; while (!stack.empty() || cur != nullptr) { while (cur != nullptr) { stack.push(cur); cur = cur->left; } TreeNode* node = stack.top(); stack.pop(); cout << node->val << " "; if (node->right != nullptr) { cur = node->right; } } }
void postOrderIteration(TreeNode* head) { if (head == nullptr) { return; } stack<TreeNode*> stack1; stack<TreeNode*> stack2; stack1.push(head); while (!stack1.empty()) { TreeNode* node = stack1.top(); stack1.pop(); stack2.push(node); if (node->left != nullptr) { stack1.push(node->left); } if (node->right != nullptr) { stack1.push(node->right); } } while (!stack2.empty()) { cout << stack2.top()->val << " "; stack2.pop(); } }
|
例 7.11 计算节点个数
1 2 3 4 5
| int Nodes(BTNode *b) { int num1, num2; if (b == NULL)return 0; else return Nodes(b->lchild) +Nodes(b->rchild)+1; }
|
例 7.12 输出叶子节点
[例7.12]假设二叉树采用二叉链存储结构存储,试设计一个算法,输出一棵给定二叉树的所有叶子结点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| void DispLeaf(BTNode *b) { if (b!=NULL) { if (b->lchild == NULL && b->rchild == NULL) printf("%c ",b->data); DispLeaf(b->lchild); DispLeaf(b->rchild); } }
void DispLeaf1(BTNode* b) { if (b != NULL) { if (b->lchild == NULL && b->rchild == NULL) printf("%c ", b->data); DispLeaf1(b->rchild); DispLeaf1(b->lchild); } }
|
例 7.13 求节点值 x 节点所在层次
1 2 3 4 5 6 7 8 9 10 11
| int Level(BTNode *b,ElemType x,int h=1) { int L; if (b == NULL)return 0; else if (b->data == x)return h; else { L = Level(b->lchild,x,h+1); if (L != 0)return 1; else return Level(b->rchild,x,h+1); } }
|
例 7.14 求第 k 层节点个数
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
| void Lnodenum(BTNode *b,int h,int k,int &n) { if (b==NULL) { return; } else { if (h == k)n++; else if (h<k) { Lnodenum(b->lchild,h+1,k,n); Lnodenum(b->rchild,h+1,k,n); } } }
int n = 0; void Lnodenum(BTNode* b, int h, int k) { if (b == NULL) { return; } else { if (h == k)n++; else if (h < k) { Lnodenum(b->lchild, h + 1, k); Lnodenum(b->rchild, h + 1, k); } } }
|