【发布时间】:2017-04-25 11:12:15
【问题描述】:
我需要使用方法inorder_tree_walk、tree_search、tree_minimum、tree_successor、tree_insert 和tree_delete 构建二叉树。当我试图编译我的程序时,我得到了一个异常mytree was nullptr。我可能对插入和删除方法有问题,程序的其他部分运行良好。我根据 Cormen 编写了这段代码。我需要任何建议,谢谢。
#include "stdafx.h"
#include <stdlib.h>
struct tree {
int key;
struct tree *parent, *left, *right;
struct tree *root;
};
void inorder_tree_walk(struct tree *x) {
if (x != NULL) {
inorder_tree_walk(x->left);
printf("%d ", x->key);
inorder_tree_walk(x->right);
}
}
struct tree *tree_search(struct tree *x, int key) {
if (x == NULL || key == x->key)
return x;
if (key < (x->key)) {
return tree_search(x->left, key);
} else {
return tree_search(x->right, key);
}
}
struct tree *tree_minimum(struct tree *x) {
while (x->left != NULL)
x = x->left;
return x;
}
struct tree *tree_successor(struct tree *x) {
struct tree *y;
if (x->right != NULL) {
return tree_minimum(x->right);
}
y = x->parent;
while (y != NULL && x == y->right) {
x = y;
y = y->parent;
return y;
}
}
struct tree *tree_insert(struct tree *mytree, int key) {
struct tree *x = NULL;
struct tree *z = NULL;
struct tree *y = NULL;
x = mytree->root;
while (x != NULL) {
y = x;
if (z->key < x->key)
x = x->left;
else
x = x->right;
}
z->parent = y;
if (y == NULL) {
mytree->root = z;
} else
if (z->key < y->key)
y->left = z;
else
y->right = z;
return 0;
}
struct tree *tree_delete(struct tree *tree, int key) {
struct tree *z = NULL;
struct tree *x;
struct tree *y;
if (z->left == NULL || z->right == NULL) {
y = z;
} else
y = tree_successor(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)
z->key = y->key;
return y;
}
int main() {
tree *root = NULL;
root = tree_insert(root, 7);
root = tree_insert(root, 13);
root = tree_insert(root, 8);
root = tree_insert(root, 23);
root = tree_insert(root, -7);
root = tree_insert(root, 13);
root = tree_insert(root, 31);
root = tree_insert(root, 5);
inorder_tree_walk(root);
printf("\n\n");
tree *tmp;
tmp = tree_minimum(root);
printf("minimum = %d\n", tmp->key);
root = tree_delete(root, 8);
root = tree_delete(root, -7);
root = tree_delete(root, 31);
inorder_tree_walk(root);
printf("\n\n");
tmp = tree_search(root, 13);
if (tmp == NULL) {
printf("not found\n");
} else {
printf("found\n");
}
getchar();
}
【问题讨论】:
-
运行程序时,您在代码中的哪个位置收到异常?
-
我不知道我的眼睛是不是在欺骗我,但是你怎么能在不调用
new、malloc等的情况下动态构建树呢?据我所知,您使用的所有指针都未初始化,这些代码都不应该工作。就好像你错过了关于动态分配的整章。另外,选择一种语言,C或C++——它们不一样。 -
第一次调用
tree_insert(),指针root = NULL;和x = mytree->root;产生异常。而且,该函数返回的唯一值是最后的return 0;。 -
Thomas Cormen 的“算法简介”是一本好书。它不是一本编程书。如果您想学习 C 或 C++(选择一个),请获得一本关于该语言的优秀编程书籍。之后,学习如何实现算法。