C++ 二叉搜索树问题
为什么搜索和继任者以及前任返回-1C++ 二叉搜索树问题,c++,binary-search-tree,C++,Binary Search Tree,为什么搜索和继任者以及前任返回-1 // BST.cpp : main project file. #include "stdafx.h" #include <cstdlib> #include <iostream> #define SIZE 10 using namespace std; struct Node { int value; Node *left; N
// BST.cpp : main project file.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#define SIZE 10
using namespace std;
struct Node {
int value;
Node *left;
Node *right;
Node *parent;
};
struct BST {
Node *root;
};
void insert(int value, BST *tree) {
Node *x = tree->root;
Node *y = NULL;
Node *z = (Node *) malloc(sizeof(Node));
z->left = NULL;
z->right = NULL;
z->value = value;
// Add your code here
while (x!=NULL){
y=x;
if (z->value < x->value)
x= x->left;
else x = x->right;
}
z->parent=y;
if (y==NULL)
tree->root=z;
else if (z->value <y->value)
y->left =z;
else y->right =z;
}
Node *search(int key, Node *n) {
if (n== NULL || key == n->value)
return n;
if (key < n->value)
search(key, n->left);
else
search(key, n->right);
}
Node *min(Node *n) {
if (n == NULL || n->left == NULL)
return n;
else
return min(n->left);
}
Node *max(Node *n) {
if (n == NULL || n->right == NULL)
return n;
else
return max(n->right);
}
Node *successor(int value, Node *n) {
Node *y = NULL;
Node *x = search(value, n);
if (x == NULL)
return NULL;
if (x->right != NULL)
return min(x->right);
y = x->parent;
while (y != NULL && x == y->right) {
x = y;
y = y->parent;
}
return y;
}
Node *predecessor(int value, Node *n) {
Node *x = search(value, n);
Node *y = NULL;
if (x == NULL)
return NULL;
if (x->left != NULL)
return max(x->left);
y = x->parent;
while (y != NULL && x == y->left) {
x = y;
y = y->parent;
}
return y;
}
Node *remove(int value, BST *tree) {
Node *z = search(value, tree->root);
Node *y = NULL, *x = NULL;
if (z == NULL) return NULL;
if (z->left == NULL || z->right == NULL)
y = z;
else
y = successor(value, z);
if (y->left != NULL)
x = y->left;
else
x = y->right;
if (x != NULL)
x->parent = y->parent;
if (y->parent == NULL)
tree->root = x;
else if (y == y->parent->left)
y->parent->left = x;
else
y->parent->right = x;
if (y != z) {
int tmp = z->value;
z->value = y->value;
y->value = tmp;
}
return y;
}
// ascending sort function
void sortAsc(Node *node) {
//Add your code here
//inorder
if (node->left!=NULL)
sortAsc(node->left);
cout<<node->value<<" ";
if (node->right!=NULL)
sortAsc(node->right);
}
// descending sort function
void sortDes(Node *node) {
// Add your code here
//inorder
if (node->right!=NULL)
sortDes(node->right);
cout<<node->value<<" ";
if (node->left!=NULL)
sortDes(node->left);
}
void clear(BST *tree) {
Node *n = NULL;
while (tree->root != NULL) {
n = remove(tree->root->value, tree);
free(n);
}
}
int main() {
int A[] = {3, 5, 10, 4, 8, 9, 1, 4, 7, 6};
Node *node = NULL;
BST *tree = (BST *) malloc(sizeof(BST));
tree->root = NULL;
// build BST tree
cout << "Input data:\n\t";
for (int i=0; i<SIZE; i++) {
cout << A[i] << " "; // by the way, print it to the console
insert(A[i], tree); // You need to complete TASK 1, so that it can work
}
// sort values in ascending order
cout << "\n\nAscending order:\n\t";
sortAsc(tree->root); // You need to complete TASK 2. Otherwise you see nothing in the console
// sort values in descending order
cout << "\n\nDescending order:\n\t";
sortDes(tree->root); // TASK 2 also!
// Find minimum value
if (tree->root != NULL)
cout << "\n\nMin: " << min(tree->root)->value;
// Find maximum value
if (tree->root != NULL)
cout << "\n\nMax: " << max(tree->root)->value;
// delete 4
cout << "\n\nDelete 4 and add 2";
//free(remove(4, tree)); // You need to complete TASK 3, so that remove(int, BST *) function works properly
// we also need to release the resource!!!
// insert 2
insert(2, tree); // It belongs to TASK 1 too.
cout << "\n\nAscending order:\n\t";
sortAsc(tree->root); // TASK 2!!
// Find the successor of 5, -1 means no successor
node = search(5, tree->root);
cout << "\n\nSearch of 5 is: " << (node != NULL?node->value:-1);
// Find the successor of 5, -1 means no successor
node = successor(5, tree->root);
cout << "\n\nSuccessor of 5 is: " << (node != NULL?node->value:-1);
// Find the predecessor of 5. -1 means no predecessor
node = predecessor(5, tree->root);
cout << "\n\nPredecessor of 5 is: " << (node != NULL?node->value:-1);
cout << "\n\n";
// clear all elements
clear(tree); // delete all nodes and release resource
free(tree); // delte the tree too
system("Pause");
}
//BST.cpp:主项目文件。
#包括“stdafx.h”
#包括
#包括
#定义尺寸10
使用名称空间std;
结构节点{
int值;
节点*左;
节点*右;
节点*父节点;
};
结构BST{
节点*根;
};
无效插入(int值,BST*树){
节点*x=树->根;
节点*y=NULL;
Node*z=(Node*)malloc(sizeof(Node));
z->left=NULL;
z->right=NULL;
z->value=value;
//在这里添加您的代码
while(x!=NULL){
y=x;
如果(z->valuevalue)
x=x->左;
否则x=x->right;
}
z->parent=y;
如果(y==NULL)
树->根=z;
else if(z->value)
y->左=z;
否则y->right=z;
}
节点*搜索(int键,节点*n){
if(n==NULL | | key==n->value)
返回n;
如果(键值)
搜索(键,n->左);
其他的
搜索(键,n->右);
}
节点*min(节点*n){
如果(n==NULL | | n->left==NULL)
返回n;
其他的
返回最小值(n->左);
}
节点*max(节点*n){
如果(n==NULL | | n->right==NULL)
返回n;
其他的
返回最大值(n->右);
}
节点*后继节点(int值,节点*n){
节点*y=NULL;
节点*x=搜索(值,n);
如果(x==NULL)
返回NULL;
如果(x->右!=NULL)
返回最小值(x->右);
y=x->父级;
而(y!=NULL&&x==y->right){
x=y;
y=y->父级;
}
返回y;
}
节点*前置节点(int值,节点*n){
节点*x=搜索(值,n);
节点*y=NULL;
如果(x==NULL)
返回NULL;
如果(x->左!=NULL)
返回最大值(x->左);
y=x->父级;
而(y!=NULL&&x==y->left){
x=y;
y=y->父级;
}
返回y;
}
节点*删除(int值,BST*树){
Node*z=搜索(值,树->根);
节点*y=NULL,*x=NULL;
如果(z==NULL)返回NULL;
如果(z->left==NULL | | z->right==NULL)
y=z;
其他的
y=后继(值,z);
如果(y->左!=NULL)
x=y->左;
其他的
x=y->右;
如果(x!=NULL)
x->parent=y->parent;
如果(y->parent==NULL)
树->根=x;
如果(y==y->父项->左项),则为else
y->父->左=x;
其他的
y->parent->right=x;
如果(y!=z){
int tmp=z->value;
z->value=y->value;
y->value=tmp;
}
返回y;
}
//升序排序函数
无效排序(节点*节点){
//在这里添加您的代码
//有序
如果(节点->左!=NULL)
排序(节点->左侧);
coutright!=NULL)
排序(节点->右侧);
Coutrout!=空){
n=删除(树->根->值,树);
自由(n);
}
}
int main(){
INTA[]={3,5,10,4,8,9,1,4,7,6};
Node*Node=NULL;
BST*树=(BST*)malloc(sizeof(BST));
树->根=NULL;
//构建BST树
cout在递归搜索启动器时存在一个bug,您需要让所有路径返回如下值:
Node *search(int key, Node *n) {
if (n== NULL || key == n->value)
return n;
if (key < n->value)
return search(key, n->left);
else
return search(key, n->right);
}
Node*搜索(int键,Node*n){
if(n==NULL | | key==n->value)
返回n;
如果(键值)
返回搜索(键,n->左);
其他的
返回搜索(键,n->右);
}
除此之外,我倾向于说,试着先调试你自己的代码,并提供更多关于你发现了什么的详细信息,而不仅仅是发布代码并询问它有什么问题。否则,你很可能会在这里得到一些真正聪明的答案;)好吧,在你的递归搜索中有一个错误,你需要返回所有路径价值观如下:
Node *search(int key, Node *n) {
if (n== NULL || key == n->value)
return n;
if (key < n->value)
return search(key, n->left);
else
return search(key, n->right);
}
Node*搜索(int键,Node*n){
if(n==NULL | | key==n->value)
返回n;
如果(键值)
返回搜索(键,n->左);
其他的
返回搜索(键,n->右);
}
除此之外,我倾向于说,试着先调试您自己的代码,并提供更多关于您发现的内容的详细信息,而不仅仅是发布代码并询问它有什么问题。否则,您可能会在这里得到一些真正聪明的答案;)