我必须根据高度平衡的树来编写带有背景的高度平衡树代码 代码如下。我必须以没有堆栈的方式修改下面的代码 在重新平衡期间再次使用以跟踪路径;而是每个非根 节点应该有一个额外的字段,指向节点的上邻居。 这些字段需要正确设置,特别是在旋转中,以及每当执行时 在叶级插入或删除。
#include <stdio.h>
#include <stdlib.h>
#define BLOCKSIZE 256
typedef int object_t;
typedef int key_t;
typedef struct tr_n_t { key_t key;
struct tr_n_t *left;
struct tr_n_t *right;
int height;
} tree_node_t;
tree_node_t *currentblock = NULL;
int size_left;
tree_node_t *free_list = NULL;
tree_node_t *get_node()
{ tree_node_t *tmp;
if( free_list != NULL )
{ tmp = free_list;
free_list = free_list -> left;
}
else
{ if( currentblock == NULL || size_left == 0)
{ currentblock =
(tree_node_t *) malloc( BLOCKSIZE * sizeof(tree_node_t) );
size_left = BLOCKSIZE;
}
tmp = currentblock++;
size_left -= 1;
}
return( tmp );
}
void return_node(tree_node_t *node)
{ node->left = free_list;
free_list = node;
}
tree_node_t *create_tree(void)
{ tree_node_t *tmp_node;
tmp_node = get_node();
tmp_node->left = NULL;
return( tmp_node );
}
void left_rotation(tree_node_t *n)
{ tree_node_t *tmp_node;
key_t tmp_key;
tmp_node = n->left;
tmp_key = n->key;
n->left = n->right;
n->key = n->right->key;
n->right = n->left->right;
n->left->right = n->left->left;
n->left->left = tmp_node;
n->left->key = tmp_key;
}
void right_rotation(tree_node_t *n)
{ tree_node_t *tmp_node;
key_t tmp_key;
tmp_node = n->right;
tmp_key = n->key;
n->right = n->left;
n->key = n->left->key;
n->left = n->right->left;
n->right->left = n->right->right;
n->right->right = tmp_node;
n->right->key = tmp_key;
}
object_t *find(tree_node_t *tree, key_t query_key)
{ tree_node_t *tmp_node;
if( tree->left == NULL )
return(NULL);
else
{ tmp_node = tree;
while( tmp_node->right != NULL )
{ if( query_key < tmp_node->key )
tmp_node = tmp_node->left;
else
tmp_node = tmp_node->right;
}
if( tmp_node->key == query_key )
return( (object_t *) tmp_node->left );
else
return( NULL );
}
}
int insert(tree_node_t *tree, key_t new_key, object_t *new_object)
{ tree_node_t *tmp_node;
int finished;
if( tree->left == NULL )
{ tree->left = (tree_node_t *) new_object;
tree->key = new_key;
tree->height = 0;
tree->right = NULL;
}
else
{ tree_node_t * path_stack[100]; int path_st_p = 0;
tmp_node = tree;
while( tmp_node->right != NULL )
{ path_stack[path_st_p++] = tmp_node;
if( new_key < tmp_node->key )
tmp_node = tmp_node->left;
else
tmp_node = tmp_node->right;
}
/* found the candidate leaf. Test whether key distinct */
if( tmp_node->key == new_key )
return( -1 );
/* key is distinct, now perform the insert */
{ tree_node_t *old_leaf, *new_leaf;
old_leaf = get_node();
old_leaf->left = tmp_node->left;
old_leaf->key = tmp_node->key;
old_leaf->right = NULL;
old_leaf->height = 0;
new_leaf = get_node();
new_leaf->left = (tree_node_t *) new_object;
new_leaf->key = new_key;
new_leaf->right = NULL;
new_leaf->height = 0;
if( tmp_node->key < new_key )
{ tmp_node->left = old_leaf;
tmp_node->right = new_leaf;
tmp_node->key = new_key;
}
else
{ tmp_node->left = new_leaf;
tmp_node->right = old_leaf;
}
tmp_node->height = 1;
}
/* rebalance */
finished = 0;
while( path_st_p > 0 && !finished )
{ int tmp_height, old_height;
tmp_node = path_stack[--path_st_p];
old_height= tmp_node->height;
if( tmp_node->left->height -
tmp_node->right->height == 2 )
{ if( tmp_node->left->left->height -
tmp_node->right->height == 1 )
{ right_rotation( tmp_node );
tmp_node->right->height =
tmp_node->right->left->height + 1;
tmp_node->height = tmp_node->right->height + 1;
}
else
{ left_rotation( tmp_node->left );
right_rotation( tmp_node );
tmp_height = tmp_node->left->left->height;
tmp_node->left->height = tmp_height + 1;
tmp_node->right->height = tmp_height + 1;
tmp_node->height = tmp_height + 2;
}
}
else if ( tmp_node->left->height -
tmp_node->right->height == -2 )
{ if( tmp_node->right->right->height -
tmp_node->left->height == 1 )
{ left_rotation( tmp_node );
tmp_node->left->height =
tmp_node->left->right->height + 1;
tmp_node->height = tmp_node->left->height + 1;
}
else
{ right_rotation( tmp_node->right );
left_rotation( tmp_node );
tmp_height = tmp_node->right->right->height;
tmp_node->left->height = tmp_height + 1;
tmp_node->right->height = tmp_height + 1;
tmp_node->height = tmp_height + 2;
}
}
else /* update height even if there was no rotation */
{ if( tmp_node->left->height > tmp_node->right->height )
tmp_node->height = tmp_node->left->height + 1;
else
tmp_node->height = tmp_node->right->height + 1;
}
if( tmp_node->height == old_height )
finished = 1;
}
}
return( 0 );
}
object_t *delete(tree_node_t *tree, key_t delete_key)
{ tree_node_t *tmp_node, *upper_node, *other_node;
object_t *deleted_object; int finished;
if( tree->left == NULL )
return( NULL );
else if( tree->right == NULL )
{ if( tree->key == delete_key )
{ deleted_object = (object_t *) tree->left;
tree->left = NULL;
return( deleted_object );
}
else
return( NULL );
}
else
{ tree_node_t * path_stack[100]; int path_st_p = 0;
tmp_node = tree;
while( tmp_node->right != NULL )
{ path_stack[path_st_p++] = tmp_node;
upper_node = tmp_node;
if( delete_key < tmp_node->key )
{ tmp_node = upper_node->left;
other_node = upper_node->right;
}
else
{ tmp_node = upper_node->right;
other_node = upper_node->left;
}
}
if( tmp_node->key != delete_key )
deleted_object = NULL;
else
{ upper_node->key = other_node->key;
upper_node->left = other_node->left;
upper_node->right = other_node->right;
upper_node->height = other_node->height;
deleted_object = (object_t *) tmp_node->left;
return_node( tmp_node );
return_node( other_node );
}
/*start rebalance*/
finished = 0; path_st_p -= 1;
while( path_st_p > 0 && !finished )
{ int tmp_height, old_height;
tmp_node = path_stack[--path_st_p];
old_height= tmp_node->height;
if( tmp_node->left->height -
tmp_node->right->height == 2 )
{ if( tmp_node->left->left->height -
tmp_node->right->height == 1 )
{ right_rotation( tmp_node );
tmp_node->right->height =
tmp_node->right->left->height + 1;
tmp_node->height = tmp_node->right->height + 1;
}
else
{ left_rotation( tmp_node->left );
right_rotation( tmp_node );
tmp_height = tmp_node->left->left->height;
tmp_node->left->height = tmp_height + 1;
tmp_node->right->height = tmp_height + 1;
tmp_node->height = tmp_height + 2;
}
}
else if ( tmp_node->left->height -
tmp_node->right->height == -2 )
{ if( tmp_node->right->right->height -
tmp_node->left->height == 1 )
{ left_rotation( tmp_node );
tmp_node->left->height =
tmp_node->left->right->height + 1;
tmp_node->height = tmp_node->left->height + 1;
}
else
{ right_rotation( tmp_node->right );
left_rotation( tmp_node );
tmp_height = tmp_node->right->right->height;
tmp_node->left->height = tmp_height + 1;
tmp_node->right->height = tmp_height + 1;
tmp_node->height = tmp_height + 2;
}
}
else /* update height even if there was no rotation */
{ if( tmp_node->left->height > tmp_node->right->height )
tmp_node->height = tmp_node->left->height + 1;
else
tmp_node->height = tmp_node->right->height + 1;
}
if( tmp_node->height == old_height )
finished = 1;
}
/*end rebalance*/
return( deleted_object );
}
}
void check_tree( tree_node_t *tr, int depth, int lower, int upper )
{ if( tr->left == NULL )
{ printf("Tree Empty\n"); return; }
if( tr->key < lower || tr->key >= upper )
printf("Wrong Key Order \n");
if( tr->right == NULL )
{ if( *( (int *) tr->left) == 10*tr->key + 2 )
printf("%d(%d) ", tr->key, depth );
else
printf("Wrong Object \n");
}
else
{ check_tree(tr->left, depth+1, lower, tr->key );
check_tree(tr->right, depth+1, tr->key, upper );
}
}
int main()
{ tree_node_t *searchtree;
char nextop;
searchtree = create_tree();
printf("Made Tree: Height-Balanced Tree\n");
while( (nextop = getchar())!= 'q' )
{ if( nextop == 'i' )
{ int inskey, *insobj, success;
insobj = (int *) malloc(sizeof(int));
scanf(" %d", &inskey);
*insobj = 10*inskey+2;
success = insert( searchtree, inskey, insobj );
if ( success == 0 )
printf(" insert successful, key = %d, object value = %d, \
height is %d\n",
inskey, *insobj, searchtree->height );
else
printf(" insert failed, success = %d\n", success);
}
if( nextop == 'f' )
{ int findkey, *findobj;
scanf(" %d", &findkey);
findobj = find( searchtree, findkey);
if( findobj == NULL )
printf(" find failed, for key %d\n", findkey);
else
printf(" find successful, found object %d\n", *findobj);
}
if( nextop == 'd' )
{ int delkey, *delobj;
scanf(" %d", &delkey);
delobj = delete( searchtree, delkey);
if( delobj == NULL )
printf(" delete failed for key %d\n", delkey);
else
printf(" delete successful, deleted object %d, height is now %d\n",
*delobj, searchtree->height);
}
if( nextop == '?' )
{ printf(" Checking tree\n");
check_tree(searchtree,0,-1000,1000);
printf("\n");
if( searchtree->left != NULL )
printf("key in root is %d, height of tree is %d\n",
searchtree->key, searchtree->height );
printf(" Finished Checking tree\n");
}
}
return(0);
}
“使用backpointers”和“不再使用堆栈”是什么意思?我是否必须修改/* start rebalancing */
部分以及函数rotation
和insert
?我有点理解高度平衡的树是如何工作的,但我并没有真正得到我为这项任务所做的事情。
答案 0 :(得分:3)
在您的起始树结构中,每个节点都有指向其左右子节点(如果有)的指针,但不指向其父节点。如果您需要在这样的树上执行操作,该树需要知道从树的根到某个感兴趣的节点的部分或全部路径,那么您需要通过遍历来构造该路径树和记录路径 - 例如,在堆栈数据结构中。您无法从末端节点向后工作。
您可以在发布的代码中看到此类行为。例如,在函数insert()
中,您有......
tree_node_t * path_stack[100]; int path_st_p = 0;
......以后......
path_stack[path_st_p++] = tmp_node;
......等等。
另一方面,如果每个节点都有一个指向其父节点的指针,则不需要跟踪树中的路径。相反,您可以从任何节点开始,并根据需要向后向上移动树,因为这样做所需的信息将由节点本身携带。赋值是要求您更改树实现以使用该方法,而不是现在使用的基于堆栈的方法。
拥有“后退”或父指针在某些方面很方便,但在其他方面则不方便。它们为许多事物提供了更简单的表达式,并且在树遍历期间需要更少的簿记。它们还可以让您在树函数之间更有效地共享代码。另一方面,无论何时何地修改树,它们都是一个额外的项目,并且它们会引入冗余,因为它们会产生不一致的机会。
您的作业首先添加一个指向struct tr_n_t
的指针。然后,无论何时向树中添加节点,都必须正确地初始化它,并在重新定义节点作为删除的直接结果或重新平衡过程时更新它。您将进一步删除insert()
和delete()
中跟踪通过树的路径到要删除的插入点/节点的代码,并修改两个函数中的重新平衡代码,以便它使用新的指向要回溯树而不是像现在一样使用堆栈。