左右旋转c中的AVL树

时间:2018-04-23 17:17:52

标签: c rotation avl-tree

我在c中实现了AVL树中左右旋转的代码,但显然它看起来像在一系列插入操作结束时不起作用我只得到第一个和最后一个插入的键我的树就像是在0-999之间插入所有偶数,如:

0,2,4,6,8,10...,998.

我只获得树中的第一个键和最后一个键,所以我的树看起来:

0
  \ 
   988

我只是想验证我写的代码是否正确并且谢谢。

struct Node{
  NodePtr left, right, parent;
  int key, height;
  int MaxInClassTree;
  int tree_size;
};

//left rotation
NodePtr real_left_rotate(NodePtr node)
{
   NodePtr y;
   y=node->right;
   node->right=y->left;
   y->left=node;
   node->parent=y;
   y->parent=node->parent;
   y->left->parent=y;
   y->right->parent=y;
   node->height=height(node);
   y->height=height(y);
   return y;
}

//the right rotation
NodePtr real_right_rotate(NodePtr node)
{
  NodePtr y;
  y=node->left;
  node->left=y->right;
  y->right=node;
  node->parent=y;
  y->parent=node->parent;
  y->left->parent=y;
  y->right->parent=y;
  node->height=height(node);
  y->height=height(y);
  return(y);  
}

 //after checking in the insertion that the node is not balanced we fix balance by this function
 void fix_balance( NodePtr node )
{
 int balance1=balance(node);
 if(node->left && balance1>1 && height(node->left->right) < height(node->left->left))
   node=real_right_rotate(node);

if(node->right && balance1 < -1 && height(node->right->right) > height(node->right->left))
   node=real_left_rotate(node);

if(node->left && balance1 > 1 && height(node->left->right) > height(node->left->left))
  {
      node->left=real_left_rotate(node->left);
      node=real_right_rotate(node);
  }

   if(node->right && balance1 < -1 && height(node->right->right)< height(node- >right->left))
   {
      node->right=right_rotate(node->right);
      node=left_rotate(node);
  }
      return;
}

 //returns the height of a node(suppose the height of a leaf is 0 and null is -1)
 int height( NodePtr node )
{
if(node==NULL)
    return -1;
return node->height;
}


//returns 1 if the node is balanced(|balance|<=1) otherwise 0
int is_balanced(NodePtr node)
{
int h;
if(node==NULL)
    return 1;
h=height(node->left) - height(node->right);
if(h<-1 || h>1)
    return 0;
return 1;
}

 //returns the balance of a node(height of the left sub-tree - height of right sub-tree)
 int balance( NodePtr node )
{
if(node==NULL)
    return 0;
return (height(node->left) - height(node->right));
 }

  NodePtr insert_AVL(NodePtr root,int x)
 {
if(root==NULL)
{
    root=get_new_node(x,REGULAR_KEY);
    root->MaxInClassTree=root->key;
    return root;
}

if(root->key==x)
    return root;

if(root->key > x )
{
   root->left=insert_AVL(root->left,x);
    root->left->parent=root;
}

if(root->key < x)
{
    root->right=insert_AVL(root->right,x);
    root->right->parent=root;
}
root->height=(MAX(height(root->left),height(root->right))+1);
if(!is_balanced(root))
    fix_balance(res_tree,root,x);
  if(root->right)
  root->MaxInClassTree=root->right->MaxInClassTree;
  else
    root->MaxInClassTree=root->key;

    if(root->left && root->right)
    root->tree_size=root->left->tree_size+root->right->tree_size+1;
else
{
    if(root->left)
        root->tree_size=root->left->tree_size+1;
    if(root->right)
        root->tree_size=root->right->tree_size+1;
}
return root;

}

0 个答案:

没有答案