写的好乱。有空再整理吧
1、 输入一系列不为零的正整数(最多不超过20个),遇到0代表输入结束(不包含0)。
2、 根据上面输入的数据序列,用初始化方法创建最大堆(不要用节点依次插入的办法创建最大堆),然后输出最大堆的层次序列。
3、 输出用堆排序后的排序结果。
4、 根据上面输入的数据,创建二叉搜索树(关键字不允许重复,如遇重复,则不重复插入该关键字),输出二叉搜索树的前序序列、中序序列(分行输出)。
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 #include <iostream> #include <queue> using namespace std;int allCount, counter = 0 ;class BStreeNode {public : int data; BStreeNode *leftChild, *rightChild; BStreeNode () { leftChild = rightChild = nullptr ; data = 0 ; } }; class MaxHeap {public : int elements[21 ]{}; int size = 0 ; void init () ; }; void MaxHeap::init () { if (size != 0 ) { for (int i = size / 2 ; i > 0 ; --i) { int currentElementLocate = i; int childLocate = 2 * i; int data ; while (childLocate <= size) { if (childLocate < size && elements[childLocate] < elements[childLocate + 1 ]) { childLocate++; } if (elements[currentElementLocate] < elements[childLocate]) { data = elements[currentElementLocate]; elements[currentElementLocate] = elements[childLocate]; elements[childLocate] = data; } currentElementLocate = childLocate; childLocate = 2 * currentElementLocate; } } } } void preOrder (BStreeNode *node) { if (node != nullptr ) { if (counter != allCount - 1 ) cout << node->data << "," ; else cout << node->data << endl; counter++; preOrder (node->leftChild); preOrder (node->rightChild); } } void inOrder (BStreeNode *node) { if (node != nullptr ) { inOrder (node->leftChild); if (counter != allCount - 1 ) cout << node->data << "," ; else cout << node->data << endl; counter++; inOrder (node->rightChild); } } void postOrder (BStreeNode *node) { if (node != nullptr ) { postOrder (node->leftChild); postOrder (node->rightChild); if (counter != allCount - 1 ) cout << node->data << "," ; else cout << node->data << endl; counter++; } } void addToBSTree (BStreeNode *pNode, int *list, int leftFrom, int leftTo, int rightFrom, int rightTo) { if (leftTo >= leftFrom || rightTo >= rightFrom) { if (leftTo >= leftFrom) { BStreeNode *leftNode = new BStreeNode (); int leftMiddle = (leftFrom + leftTo) / 2 ; leftNode->data = list[leftMiddle]; if (pNode->data != leftNode->data) { pNode->leftChild = leftNode; } addToBSTree (leftNode, list, leftFrom, leftMiddle - 1 , leftMiddle + 1 , leftTo); } if (rightTo >= rightFrom) { BStreeNode *rightNode = new BStreeNode (); int rightMiddle = (rightFrom + rightTo) / 2 ; rightNode->data = list[rightMiddle]; if (pNode->data != rightNode->data) { pNode->rightChild = rightNode; } addToBSTree (rightNode, list, rightFrom, rightMiddle - 1 , rightMiddle + 1 , rightTo); } } } void find (BStreeNode *root, BStreeNode *node) { BStreeNode *current = root; if (node->data > current->data) { if (current->rightChild != nullptr ) { current = current->rightChild; find (current, node); } else { current->rightChild = node; } } else if (node->data < current->data) { if (current->leftChild != nullptr ) { current = current->leftChild; find (current, node); } else { current->leftChild = node; } } } int main () { cout << "Input" << endl; MaxHeap *heap = new MaxHeap (); int inputData[21 ]; bool has; int locate = 0 ; int data = 0 ; cin >> data; inputData[allCount] = data; allCount++; while (data != 0 ) { locate++; heap->elements[locate] = data; cin >> data; has = false ; for (int i = 0 ; i < allCount; ++i) { if (data == inputData[i]) { has = true ; } } if (!has && data != 0 ) { inputData[allCount] = data; allCount++; } } heap->size = locate; heap->init (); cout << "Output" << endl; for (int i = 1 ; i < heap->size; ++i) { cout << heap->elements[i] << "," ; } cout << heap->elements[heap->size] << endl; int counts = heap->size; int result[counts]; locate = counts - 1 ; for (int i = 1 ; i <= counts; ++i) { result[locate] = heap->elements[1 ]; for (int j = 1 ; j < heap->size; ++j) { heap->elements[j] = heap->elements[j + 1 ]; } heap->size--; locate--; heap->init (); } for (int i = 0 ; i < counts - 1 ; ++i) { cout << result[i] << "," ; } cout << result[counts - 1 ] << endl; BStreeNode *root = new BStreeNode (); root->data = inputData[0 ]; for (int i = 1 ; i < allCount; ++i) { BStreeNode *node = new BStreeNode (); node->data = inputData[i]; find (root, node); } preOrder (root); counter = 0 ; inOrder (root); cout << "End" << endl; return 0 ; }