【问题标题】:How to transform the tree into a backbone with left rotations in C AVL tree如何在 C AVL 树中将树转换为具有左旋转的主干
【发布时间】:2021-12-20 07:19:01
【问题描述】:

我尝试将树转换为左旋转的主干 树的东西正在工作,但左旋转功能存在一些问题

#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");
  }
}

在这里我创建结构类型和节点初始化

我在下面定义了 bst 插入函数

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;
        }
      }
    }
  }
}

我横向定义打印树

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);
  }
}

这是我的左循环递归函数

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);
    }
  }
}

这是创建树和一些节点的主要功能

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;
}

【问题讨论】:

    标签: c data-structures binary-tree avl-tree recursive-datastructures


    【解决方案1】:

    一些问题:

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

    这里是一个更正:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-18
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多