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
| #include<iostream> #include<vector> #include<string> using namespace std; template <class T> class Bitree { public: void createBitree(); void chengchiDisplay(); void preDisplay(int ptr); void midDisplay(int ptr); void backDisplay(int ptr); void buildByPreAndMid(); T handleBuildByPreAndBack(vector<T> preArr, int pl, int pr, vector<T> midArr, int il, int ir); int findRoot(T root, vector<T> midArr, int il, int ir); public: vector<T> tree; int treeNodeNumber; }; template <class T> void Bitree<T>::createBitree() { cout << "请输入结点的个数:"; cin >> this->treeNodeNumber; this->tree.resize(this->treeNodeNumber + 1); cout << endl; cout << "请按顺序输入相关结点的值:"; int tempNum = this->treeNodeNumber; T tempTreeNode; for(int i = 1; i <= this->treeNodeNumber; i++) { cin >> tempTreeNode; tree[i] = tempTreeNode; } }
template <class T> void Bitree<T>::chengchiDisplay() { for(int i = 1; i <= this->treeNodeNumber; i++) { cout << this->tree[i] << " "; }cout << endl; }
template <class T> void Bitree<T>::preDisplay(int ptr) { if(ptr <= this->treeNodeNumber) { cout << this->tree[ptr] << " "; preDisplay(2 * ptr); preDisplay(2 * ptr + 1); } }
template <class T> void Bitree<T>::midDisplay(int ptr) { if(ptr <= this->treeNodeNumber) { midDisplay(2 * ptr); cout << this->tree[ptr] << " "; midDisplay(2 * ptr + 1); } }
template <class T> void Bitree<T>::backDisplay(int ptr) { if(ptr <= this->treeNodeNumber) { backDisplay(2 * ptr); backDisplay(2 * ptr + 1); cout << this->tree[ptr] << " "; } }
template <class T> int Bitree<T>::findRoot(T root, vector<T> midArr, int il, int ir) { for(int i = il; i <= ir; i++) { if(midArr[i] == root) { return i; } } return -1; }
template <class T> T Bitree<T>::handleBuildByPreAndBack(vector<T> preArr, int pl, int pr, vector<T> midArr, int il, int ir) { if(pl == 1) this->tree[pl] = preArr[pl]; T root = preArr[pl]; int k = this->findRoot(preArr[pl], midArr, il, ir); if (k > il && 2 * pl < preArr.size()) { this->tree[2 * pl] = handleBuildByPreAndBack(preArr, pl + 1, k - il + pl, midArr, il, k - 1); } if (k < ir && 2 * pl + 1 < preArr.size()) { this->tree[2 * pl + 1] = handleBuildByPreAndBack(preArr, k - il + pl + 1, pr, midArr, k + 1, ir); } return root; }
template <class T> void Bitree<T>::buildByPreAndMid() { cout << "请输入结点的个数:"; int n; cin >> n; this->tree.resize(n + 1); this->treeNodeNumber = n; vector<T> preArr(n + 1); vector<T> midArr(n + 1); cout << endl; cout << "请输入中序遍历序列:"; T temp; for(int i = 1; i <= n; i++) { cin >> temp; midArr[i] = temp; }cout << endl; cout << "请输入先序遍历序列:"; for(int i = 1; i <= n; i++) { cin >> temp; preArr[i] = temp; }cout << endl; this->handleBuildByPreAndBack(preArr, 1, n, midArr, 1, n); }
int main() { Bitree<int> T; T.buildByPreAndMid(); cout << "层次遍历:"; T.chengchiDisplay(); cout << "先序遍历:"; T.preDisplay(1);cout << endl; cout << "中序遍历:"; T.midDisplay(1);cout << endl; cout << "后序遍历:"; T.backDisplay(1);cout << endl; return 0; }
|