C++實現二叉排序樹(順序儲存)
阿新 • • 發佈:2020-12-24
1、任務描述:編寫函式,建立有序表,利用二叉排序樹的插入演算法建立二叉排序樹;在以上二叉排序樹中刪除某一指定關鍵字元素;採用折半查詢實現某一已知的關鍵字的查詢(採用順序表儲存結構)
2、亮點:無亮點 我覺得用順序表寫樹是一個很蠢的事情
3、執行情況(測試資料見程式碼第8行)
PS:由於最後輸入了^Z 所以無法讀取要刪除的數,如果有大佬知道在VS2019中如何沖走緩衝區的結束符請告訴我
4、原始碼
#include <iostream>
#include <stack>
#define ElemType int
using namespace std;
//測試資料 30 15 41 33 50 35 34 ^Z
class BinSortTree {
private:
class BinNode {
public:
ElemType date;
int left;
int right;
};
BinNode* head;
int length = 3;
const int null = -1;
const int right = 1;
const int left = 0;
public:
BinSortTree() {
head = new BinNode[length] ;
head[0].left = null;
head[0].right = null;
for (int i = 1; i < length; i++) {
head[i].left = null;
head[i].right = null;
head[i].date = INT_MIN;
}
}
void ReSize() {
BinNode* tmp = new BinNode[length * 2];
for (int i = 0; i < length; i++) {
tmp[i] = head[i];
}
delete [] head;
head = tmp;
for (int i = length; i < length * 2; i++) {
head[i].left = null;
head[i].right = null;
head[i].date = INT_MIN;
}
length *= 2;
}
//中序遍歷 輸出排序好的資料
void PreTravel(int pos) {
if (null == pos)
return;
PreTravel(head[pos].left);
cout << head[pos].date << " ";
PreTravel(head[pos].right);
}
void DisPlay() {
PreTravel(1);
cout << endl;
}
bool IsEmpty() {
return null == head->left && null == head->right ? true : false;
}
void Creat() {
int num;
cout << "請輸入資料: ";
while (cin >> num) {
int pos = 1, pre = pos;
//尋找這個資料應該在的結點
while (INT_MIN != head[pos].date || null != head[pos].left || null != head[pos].right) {
pre = pos;
pos = num > head[pos].date ? 2 * pos + 1 : 2 * pos;
while (pos >= length)
ReSize();
}
head[pos].date = num;
if (2 * pre == pos)
head[pre].left = pos;
else if ((2 * pre + 1 == pos))
head[pre].right = pos;
// cout << "請繼續輸入資料:";
}
}
//判斷pos位置的元素在上一個元素的左邊還是右邊
int GetDirection(int pos) {
return head[pos].date > head[pos / 2].date ? right : left;
}
int Find(ElemType e) {
stack<int> s;
s.push(1);
while (!s.empty()) {
int pos = s.top();
s.pop();
if (e == head[pos].date)
return pos;
if (null != head[pos].left)
s.push(head[pos].left);
if (null != head[pos].right)
s.push(head[pos].right);
}
return false;
}
void Delete(ElemType e) {
int pos = Find(e);
if (false == pos) {
cout << e << "不存在" << endl;
return;
}
cout << e << "刪除成功" << endl;
while (false != pos) {
if (null == head[pos].left && null == head[pos].right) {
if (GetDirection(pos) == right)
head[pos / 2].right = null;
else
head[pos / 2].left = null;
pos = Find(e);
continue;
}
if (null == head[pos].left) {
if (GetDirection(pos) == right)
head[pos / 2].right = head[pos].right;
else
head[pos / 2].left = head[pos].right;
head[pos].right = null;
pos = Find(e);
continue;
}
if (null == head[pos].right) {
if (GetDirection(pos) == right)
head[pos / 2].right = head[pos].left;
else
head[pos / 2].left = head[pos].left;
head[pos].left = null;
pos = Find(e);
continue;
}
int min = head[pos].right, pre = pos;
while (null != head[min].left) {
pre = min;
min = head[min].left;
}
if(pos != pre)
head[pre].left = head[min].right;
head[min].right = null;
if (left == GetDirection(min))
head[pre].left = null;
else
head[pre].right = null;
head[pos].date = head[min].date;
pos = Find(e);
}
}
};
int main() {
BinSortTree* t = new BinSortTree;
t->Creat();
t->DisPlay();
t->Delete(41);
t->DisPlay();
}