如何在 C AVL 树中将树转换为左旋转的 backbone

How to transform the tree into a backbone with left rotations in C AVL tree

I tried to convert the tree into a backbone with left rotations Tree stuffs is working but there some problems in left rotate function

#include <stdio.h>
#include <stdlib.h>

typedef struct NODE_s *NODE;
typedef struct NODE_s
{
  NODE left;
  NODE right;
  void *data;
  int key;
} NODE_t[1];

typedef struct
{
  NODE root;
} BST_t[1], *BST;

BST bst_init()
{
  BST tree = (BST)malloc(sizeof(BST_t));
  tree->root = NULL;
  return tree;
}

void bst_kill(BST tree)
{
  // ??????
  free(tree);
}

NODE bst_init_node(int key, void *data)
{
  NODE node = (NODE)malloc(sizeof(NODE_t));
  node->left = NULL;
  node->right = NULL;
  node->data = data;
  node->key = key;
  return node;
}

void bst_kill_node(NODE node)
{
  if (node != NULL)
  {
    free(node);
  }
  else
  {
    printf("ERROR: Uninitialized node!\n");
  }
}

in here I create struct type and node initializing

I have define bst insert function in the below

void bst_insert(BST tree, int key, void *data)
{
  if (tree == NULL)
  {
    printf("ERROR: Tree is not initialized");
  }
  else
  {
    NODE node = tree->root;
    if (node == NULL)
    {
      tree->root = bst_init_node(key, data);
    }
    else
    {
      while (1)
      {
        if (key < node->key)
        {
          if (node->left != NULL)
          {
            node = node->left;
          }
          else
          {
            node->left = bst_init_node(key, data);
            break;
          }
        }
        else if (key > node->key)
        {
          if (node->right != NULL)
          {
            node = node->right;
          }
          else
          {
            node->right = bst_init_node(key, data);
            break;
          }
        }
        else
        {
          printf("ERROR: Duplicate key not allowed.\n");
          break;
        }
      }
    }
  }
}

I define print tree horizantally

void printTree(NODE node, int a)
{

  int i;
  if (node != NULL)
  {
    printTree(node->right, a + 5);
    for (i = 0; i < a; i++)
    {
      printf("  ");
    }
    printf("%d\n", node->key);
    printTree(node->left, a + 5);
  }
}

Heres my left rotate function recursivly

void left_rotate(NODE node)
{
  if (node != NULL)
  {


    NODE parent, left_child, left_child_right;

    if (node->left != NULL)
    {

      parent = node;
      left_child = parent->left;
      if (left_child->right != NULL)
      {
        left_child_right = left_child->right;

        parent->left = left_child_right;

      }
      else
      {
        left_child->right = parent;
      }
      left_rotate(left_child);
    }
  }
}

Heres the main function to create tree and some nodes

int main()
{
  BST tree1 = bst_init();

  bst_insert(tree1, 55, NULL);
  bst_insert(tree1, 21, NULL);
  bst_insert(tree1, 76, NULL);
  bst_insert(tree1, 13, NULL);
  bst_insert(tree1, 38, NULL);
  bst_insert(tree1, 64, NULL);
  bst_insert(tree1, 89, NULL);
  bst_insert(tree1, 3, NULL);
  bst_insert(tree1, 33, NULL);
  bst_insert(tree1, 47, NULL);
  bst_insert(tree1, 72, NULL);
  bst_insert(tree1, 41, NULL);
  bst_insert(tree1, 53, NULL);
  bst_insert(tree1, 50, NULL);
  bst_insert(tree1, 25, NULL);



  left_rotate(tree1->root);


  printTree(tree1->root, 0);

  bst_kill(tree1);

  return 1;
}

一些问题:

  • 您的轮换代码中缺少一些作业。
  • 只要节点有权利 child,就应该重复相同的旋转,所以 if 应该变成 while
  • 该函数应该 return 旋转到顶部的节点,以便调用者可以对 parent 节点的 child 引用进行必要的分配。

这里更正:

NODE left_rotate(NODE node)
{
  if (node != NULL)
  {
    while (node->left != NULL) {
      NODE left_child = node->left;;
      NODE left_child_right = left_child->right;
      left_child->right = node;
      node->left = left_child_right;
      node = left_child;
    }
    node->right = left_rotate(node->right);
  }
  return node;
}

不是你的问题,但你的 kill 函数也需要更正:

void bst_kill_node(NODE node)
{
  if (node == NULL) {
    return;
  }
  // Free descendants 
  bst_kill_node(node->left);
  bst_kill_node(node->right);
  // ...and finally the node itself
  free(node);
}

void bst_kill(BST tree)
{
  // free the root node if it exist
  bst_kill_node(tree->root);
  free(tree);
}