TAG 此代码遇到一个bug,在Insert函数中,注释部分,思考一下为什么用这个方法来添加会失效
#include<iostream> using namespace std; struct BTNode { int data; BTNode *lchild,*rchild; }; void selectsort(int a[], int n) { for (int i = 1; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] > a[j]) { int temp = a[j]; a[j] = a[i]; a[i] = temp; } } } } class BTree { private: BTNode *root; void creat(int loc, BTNode *p); void travel(BTNode *p); int *data, number; void search(int key, BTNode *p, bool &flag); void insert(BTNode *p, int x); public: BTree(int a[], int n); void Creat(); void Insert(int x); void Search(int key); void print(); }; BTree::BTree(int a[], int n) { int mid = a[n / 2]; root = new BTNode(); root->data = mid; root->lchild = root->rchild = NULL; number = n; data = new int[number]; for (int i = 0; i < number; i++) { data[i] = a[i]; } selectsort(data, number); } void BTree::Insert(int x) { BTNode *p = root; insert(p, x); cout << "插入完成!" << endl; } void BTree::insert(BTNode *p, int x) { /*if (p==NULL) { p = new BTNode(); p->data = x; p->rchild = p->lchild = NULL; cout << "p->data" <<p->data<< endl; } else if (x > p->data) { insert(p->rchild, x); } else if (x < p->data) { insert(p->lchild, x); }*/ if (x > p->data) { if (p->rchild == NULL) { p->rchild = new BTNode(); p->rchild->data = x; p->rchild->rchild = p->rchild->lchild = NULL; cout << "p->data" << p->rchild->data << endl; } else insert(p->rchild, x); } else if (x < p->data) { if (p->lchild == NULL) { p->lchild = new BTNode(); p->lchild->data = x; p->lchild->rchild = p->lchild->lchild = NULL; cout << "p->data" << p->lchild->data << endl; } else insert(p->lchild, x); } } void BTree::print() { BTNode *p = root; travel(p); cout << endl; } void BTree::search(int key, BTNode *p,bool &flag) { while (p) { if (p->data == key) { flag = true; break; } else if (p->data>key) { p = p->lchild; } else { p = p->rchild; } } } void BTree::creat(int loc, BTNode * p) { if (data[loc] > p->data) { if (p->rchild == NULL) { p->rchild = new BTNode(); p->rchild->data = data[loc]; p->rchild->lchild = p->rchild->rchild = NULL; } else { creat(loc, p->rchild); } } else if (data[loc] < p->data) { if(p->lchild == NULL) { p->lchild = new BTNode(); p->lchild->data = data[loc]; p->lchild->lchild = p->lchild->rchild = NULL; } else { creat(loc, p->lchild); } } } void BTree::travel(BTNode * p) { if (p != NULL) { travel(p->lchild); cout << p->data << " "; travel(p->rchild); } } void BTree::Creat() { BTNode *p = root; for (int i = 0; i < number; i++) { creat(i, p); } cout << "二叉搜索树创建成功了。。。吧" << endl; } void BTree::Search(int key) { BTNode *p = root; bool flag = false; search(key, p, flag); if (flag) { cout << "搜索到" << key << "在二叉树中" << endl; } else { cout << "未查找到指定数据!" << endl; } } int main() { cout << "请输入二叉搜索树的元素个数:"; int number, *a, key, x; cin >> number; a = new int[number]; cout << "请分别为这些元素赋值:" << endl; for (int i = 0; i < number; i++) { cin >> a[i]; } BTree test(a, number); test.Creat(); test.print(); cout << "请输入要查找的数值" << endl; cin >> key; test.Search(key); cout << "请输入要插入的数值" << endl; cin >> x; test.Insert(x); test.print(); system("pause"); return 0; }
主要还是因为指针的问题。
在注释中,是判断p是否为空来创建,但是注意这个NULL是随机的,虽然p空这个值是叶子节点的孩子赋值的,但是再用p来开辟空间就已经完全和二叉树没有关系了,所以在打印的时候找不到
而在@陈总 的解决办法中,是直接通过先判断大小再判断它下面的左右指针是否为空来进行插入(因为你是知道左小右大的,所以只用判断一个),然后再用p->lchild或者p->rchild来开辟空间就避免出现这种问题。
当然注释的那种解决方案也是有的,你需要记录一下叶子结点的地址,然后把p连接上去···额
原文地址:https://www.cnblogs.com/robotpaul/p/10152476.html
时间: 2024-10-10 17:31:57