Algorithm - 数据结构 - BinaryTree
参考链接:
http://blog.51cto.com/9291927/2083190
http://www.cnblogs.com/polly333/p/4740355.html
树形结构
template<class T> struct BinaryTreeNode { T value; int depth; BinaryTreeNode * left, * right, * parent; BinaryTreeNode() {} BinaryTreeNode(T v) { value = v; } BinaryTreeNode(T v, int d) { value = v; depth = d; } }; #define INT_MAX 0 bool isFirst; template<class T> struct BinaryTree { T * arr; int length, depth; BinaryTreeNode<T> * head; public: BinaryTree() {} BinaryTree(T * ar, int len) { int i; depth = 1; length = len; arr = new T[length]; for (int i = 0; i < length; i++) arr[i] = ar[i]; head = new BinaryTreeNode<T>(ar[0], 1); InsertNode(head, 0); } // 添加节点 void InsertNode(BinaryTreeNode<T> * node, int index); // 先序遍历 void PreOrder(BinaryTreeNode<T> * node); // 中序遍历 void InOrder(BinaryTreeNode<T> * node); // 后续遍历 void PostOrder(BinaryTreeNode<T> * node); };
插入
template<class T> void BinaryTree<T>::InsertNode(BinaryTreeNode<T> * node, int index) { node -> left = NULL; node -> right = NULL; int left = 2 * index + 1; if (left < length) { if (arr[left] != -INT_MAX) { node -> left = new BinaryTreeNode<T>(arr[left], node -> depth + 1); depth = depth > node -> left -> depth ? depth : node -> left -> depth; InsertNode(node -> left, left); } } int right = 2 * index + 2; if (right < length) { if (arr[right] != -INT_MAX) { node -> right = new BinaryTreeNode<T>(arr[right], node -> depth + 1); depth = depth > node -> right -> depth ? depth : node -> right -> depth; InsertNode(node -> right, right); } } }
遍历
// 先序遍历 // 根节点 > 左节点 > 右节点 template<class T> void BinaryTree<T>::PreOrder(BinaryTreeNode<T> * node) { if (node != NULL) { if (node != head) cout << " "; cout << node -> value; PreOrder(node -> left); PreOrder(node -> right); } } // 中序遍历 // 左节点 > 根节点 > 右节点 template<class T> void BinaryTree<T>::InOrder(BinaryTreeNode<T> * node) { if (node != NULL) { InOrder(node -> left); cout << node -> value; InOrder(node -> right); } } // 后序遍历 // 左节点 > 右节点 > 根节点 template<class T> void BinaryTree<T>::PostOrder(BinaryTreeNode<T> * node) { if (node != NULL) { PostOrder(node -> left); PostOrder(node -> right); if (!isFirst) cout << " "; cout << node -> value; isFirst = false; } }
繁茂度:
一棵树, 每一层的 层数和该层节点数量的乘积之和叫做该层的繁茂度
测试代码:
int main() { int i, t; int * arr = new int[1005]; scanf("%d", &t); while (t --) { i = 0; while (scanf("%d", &arr[i]), arr[i] != -1) { i++; } BinaryTree * tree = new BinaryTree(arr, i); cout << tree -> depth << " "; tree -> PreOrder(tree -> head); cout << endl; } return 0; }
线性结构
测试代码:
int main() { int tree[MAX], index = 0; int input; while(scanf("%d", &input), input != -1) { tree[index ++] = input; } return 0; }
插入
遍历:
求高度:
数的高度是他最大元素所在的位置的 Log2 + 1,
用数学表达式就是 Log2(最大节点的Index) + 1
#include <stdio.h> #include <math.h> int Height(int * tree, int len){ int result = (int)log2(len); return result + 1; }
求宽度:
宽度代表二叉树 每一层有最大节点的数量
int Width(int * tree, int len){ int i = 0; int max = 0; int depth = 0; int sum = 0; for(; i < len; i ++){ int currentDepth = (int)log2(i + 1) + 1; // 当前高度等于当前索引值的 log(2) + 1 if(depth == currentDepth){ sum += tree[i] != 0; } else{ sum = tree[i] != 0; depth = currentDepth; } max = sum > max ? sum : max; } return max; }
重建二叉树
推算中序遍历:
已知先序遍历和后序遍历, 推算中序遍历有几种可能性的算法
int main() { int t, result = 1; char * s1 = new char[101], * s2 = new char[101]; char x, y, z; scanf("%d", &t); while(t --){ int i, j; result = 1; scanf("%s %s", s1, s2); int length = strlen(s1); for(i = 0; i < length - 1; i ++){ x = s1[i]; y = s1[i + 1]; for(j = 1; j < length; j ++) if(x == s2[j]) break; z = s2[j - 1]; if(y == z) result *= 2; } printf("%d\n", result); //system("pause"); } return 0; }
广义表结构
繁茂度:
一棵树, 每一层的 层数和该层节点数量的乘积之和叫做该层的繁茂度
// 用广义表表示方法求繁茂度 int t, result = 0, depth = 0; char node; scanf("%d", &t); getchar(); while(t--){ result = 0; while(scanf("%c", &node) != EOF){ if(node == '\n') break; switch(node){ case ' ' : break; case ',' : break; case '(': depth ++; break; case ')': depth --; break; default: result += depth * 1; break; } } printf("%d\n", result); }