C 二叉搜索树索引
我在从二叉树中获取特定索引处的元素时遇到问题。我遇到问题的函数是泛型tree\u get\u at\u index(tree\u node*t,int index){ 分配要求我在二叉树中的特定索引处查找元素。例如,0索引应返回二叉树中最低的元素,index=treesize应返回树中最大的元素。我的树中有一个大小函数,可以正常工作,但由于某些原因,我无法使索引正常工作。任何帮助p将不胜感激。谢谢 现在,在树运行一次之后,我遇到了seg故障C 二叉搜索树索引,c,tree,C,Tree,我在从二叉树中获取特定索引处的元素时遇到问题。我遇到问题的函数是泛型tree\u get\u at\u index(tree\u node*t,int index){ 分配要求我在二叉树中的特定索引处查找元素。例如,0索引应返回二叉树中最低的元素,index=treesize应返回树中最大的元素。我的树中有一个大小函数,可以正常工作,但由于某些原因,我无法使索引正常工作。任何帮助p将不胜感激。谢谢 现在,在树运行一次之后,我遇到了seg故障 #include "tree.h" #inclu
#include "tree.h"
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
/* Memory Management */
/* This constructor returns a pointer to a new tree node that contains the
* given element.*/
tree_node* new_tree_node(generic e) {
/*TODO: Complete this function!*/
tree_node* to_return = malloc(sizeof(tree_node));
to_return->element = e;
to_return->left = NULL;
to_return->right = NULL;
return to_return;
}
/* This function is expected to free the memory associated with a node and all
* of its descendants.*/
void free_tree(tree_node* t) {
/*TODO: Complete this function!*/
if (t != NULL){
free_tree(t->left);
free_tree(t->right);
free(t);
}
}
/* End Memory Management */
/* Tree Storage and Access */
bool tree_contains(tree_node* t, generic e) {
/*TODO: Complete this function!*/
/*
if (t == NULL || t->element != e) {
return false;
}
else if (t->element == e) {
return true;
}
return tree_contains(t,e);
}
*/
if(t == NULL )
return false;
else if(t->element == e)
return true;
else if (e<t->element)
return tree_contains(t->left,e);
else
return tree_contains(t->right,e);
}
tree_node* tree_add(tree_node* t, generic e) {
/*TODO: Complete this function!*/
if(t==NULL)
t = new_tree_node(e);
else if(e == t->element)
return t;
else if(e > (t->element))
{
t->right = tree_add(t->right,e);
}
else if(e < (t->element))
{
t->left = tree_add(t->left,e);
}
return t;
}
tree_node* tree_remove(tree_node* t, generic e) {
/*TODO: Complete this function!*/
if (t == NULL) return t;
else if (e < t->element)
t->left = tree_remove(t->left, e);
else if (e > t->element)
t->right = tree_remove(t->right, e);
else
{
if (t->left == NULL)
{
tree_node *temp = t->right;
free(t);
return temp;
}
else if (t->right == NULL)
{
tree_node *temp = t->left;
free(t);
return temp;
}
else {
tree_node* current = t->right;
tree_node* temp = t->right;
while (current->left != NULL)
current = current->left;
t->element = current->element;
while (temp->left->left != NULL)
temp = temp->left;
temp->left = current->right;
free(current);
}
}
return t;
}
/* End Tree Storage and Access */
/* Size and Index */
/* Return the size of the tree rooted at the given node.
* The size of a tree is the number of nodes it contains.
* This function should work on subtrees, not just the root.
* If t is NULL, it is to be treated as an empty tree and you should
* return 0.
* A single node is a tree of size 1.*/
int tree_size(tree_node* t) {
/*TODO: Complete this function!*/
if (t==NULL)
return 0;
else
return(tree_size(t->left) + 1 + tree_size(t->right));
}
/* Return the element at the given index in the given tree.
* To be clear, imagine the tree is a sorted array, and you are
* to return the element at the given index.
*
* Assume indexing is zero based; if index is zero then the minimum
* element should be returned, for example. If index is one then
* the second smallest element should bereturned, and so on.*/
generic tree_get_at_index(tree_node* t, int index) {
//assert(index >=0 && index < tree_size(t));
/*TODO: Complete this function!*/
//tree_node* new_node = t;
// int min = 0;
// int max = tree_size(t);
// int current = (min+max)/2;
int current = index;
printf("tree size: %d \n", tree_size(t));
//while( new_node != NULL){
if(current == (tree_size(t)-1)){
return t->element;
printf("index = tree size \n");
}
else if(index < (tree_size(t->left))){
//current--;
return tree_get_at_index(t->left, index);
printf("index < tree size \n"); //= new_node->right;
}
else if(index > (tree_size(t->left))){
return tree_get_at_index(t->right, index);
printf("index > tree size \n");
}
return t->element;
//return (generic)0;
}
/* End Size and Index */
#包括“tree.h”
#包括
#包括
#包括
#包括
#包括
/*内存管理*/
/*此构造函数返回指向包含
*给定元素*/
树节点*新树节点(通用e){
/*TODO:完成此功能*/
tree_node*to_return=malloc(sizeof(tree_node));
返回->元素=e;
to_return->left=NULL;
to_return->right=NULL;
返回到_返回;
}
/*此函数将释放与节点和所有节点相关的内存
*它的后代*/
无效自由树(树节点*t){
/*TODO:完成此功能*/
如果(t!=NULL){
自由树(t->左);
自由树(t->右);
自由(t);
}
}
/*终端内存管理*/
/*树存储和访问*/
布尔树包含(树节点*t,泛型e){
/*TODO:完成此功能*/
/*
if(t==NULL | | t->element!=e){
返回false;
}
否则如果(t->element==e){
返回true;
}
返回树包含(t,e);
}
*/
如果(t==NULL)
返回false;
否则如果(t->element==e)
返回true;
else if(元素)
返回树包含(t->left,e);
其他的
返回树包含(t->右,e);
}
树节点*树添加(树节点*t,通用e){
/*TODO:完成此功能*/
如果(t==NULL)
t=新的树节点(e);
else if(e==t->element)
返回t;
否则如果(e>(t->元素))
{
t->right=tree\u add(t->right,e);
}
否则如果(e<(t->元素))
{
t->left=树添加(t->left,e);
}
返回t;
}
树节点*树删除(树节点*t,通用e){
/*TODO:完成此功能*/
如果(t==NULL)返回t;
else if(eelement)
t->left=tree\u remove(t->left,e);
否则如果(e>t->元素)
t->right=tree\u remove(t->right,e);
其他的
{
如果(t->left==NULL)
{
树节点*temp=t->右侧;
自由(t);
返回温度;
}
else if(t->right==NULL)
{
树节点*temp=t->左;
自由(t);
返回温度;
}
否则{
树节点*current=t->右侧;
树节点*temp=t->右侧;
while(当前->左!=NULL)
当前=当前->左侧;
t->element=当前->元素;
while(临时->左->左!=NULL)
温度=温度->左侧;
温度->左=当前->右;
自由(电流);
}
}
返回t;
}
/*端树存储和访问*/
/*大小和索引*/
/*返回给定节点上根目录树的大小。
*树的大小是它包含的节点数。
*此函数应适用于子树,而不仅仅是根。
*如果t为NULL,它将被视为一个空树,您应该
*返回0。
*单个节点是大小为1的树*/
整数树大小(树节点*t){
/*TODO:完成此功能*/
如果(t==NULL)
返回0;
其他的
返回(树大小(t->左)+1+树大小(t->右));
}
/*返回给定树中给定索引处的元素。
*为了清楚起见,假设树是一个排序数组,而您是
*返回给定索引处的元素。
*
*假设索引是零基的;如果索引为零,则为最小值
*例如,应该返回元素。如果索引为1,则
*第二个最小的元素应该被旋转,依此类推*/
泛型树在索引处获取(树节点*t,int索引){
//断言(索引>=0&&indexelement;
printf(“索引=树大小\n”);
}
else if(索引<(树大小(t->左))){
//当前--;
返回树在索引处获取(t->left,index);
printf(“indexright;
}
else if(索引>(树大小(t->left))){
返回树在索引处获取(t->右,索引);
printf(“索引>树大小\n”);
}
返回t->element;
//返回(通用)0;
}
/*结束大小和索引*/
通用树在索引处获取(树节点*t,int索引){
assert(index>=0&&index我们将尝试填充一个虚拟数组,因为您知道可以跳过索引的每个子树的大小
generic tree_get_at_index(tree_node* t, int index) {
// sanity check
assert(t);
assert(index > 0);
int leftCount=tree_size(t->left);
if(index < leftCount ) {
// good chance that the node we seek is in the left children
return tree_get_at_index(t->left, index);
}
if(index==leftCount) {
// looking at the "middle" of the sub tree
return t->element;
}
// else look at the right sub tree as it was its own array
return tree_get_at_index(t->right, index - leftCount - 1);
}
generic tree\u get\u at\u index(tree\u node*t,int index){
//健康检查
断言(t);
断言(索引>0);
int leftCount=树的大小(t->left);
如果(索引<左计数){
//我们寻找的节点很有可能位于左侧的子节点中
返回树在索引处获取(t->left,index);
}
如果(索引==leftCount){
//查看子树的“中间”
返回t->element;
}
//否则,请查看右边的子树,因为它是自己的数组
返回树在索引处获取(t->right,index-leftCount-1);
}
请更好地描述您的具体问题,而不是“我因某种原因无法使其正常工作”。请提供测试输入、预期行为和实际行为。有关如何改进您的解决方案的更多指导,请查看
generic tree_get_at_index(tree_node* t, int index) {
// sanity check
assert(t);
assert(index > 0);
int leftCount=tree_size(t->left);
if(index < leftCount ) {
// good chance that the node we seek is in the left children
return tree_get_at_index(t->left, index);
}
if(index==leftCount) {
// looking at the "middle" of the sub tree
return t->element;
}
// else look at the right sub tree as it was its own array
return tree_get_at_index(t->right, index - leftCount - 1);
}