avl树帮助字典实现

发布于 2024-10-17 12:28:47 字数 2650 浏览 4 评论 0原文

我目前正在学习c和算法。 我已经实现了一个模拟字典作为一棵树,并且希望我的课程工作更进一步并使用 avl 树。

  #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define __USE_BSD
#include <string.h>

#include "speller.h"
#include "dict.h"

typedef struct node * tree_ptr;
struct node {
  Key_Type element; // only data is the key itself
  tree_ptr left, right;
  // add anything else that you need
};

struct table {
  tree_ptr head; // points to the head of the tree
  // add anything else that you need
}dictable;

Table initialize_table(/*ignore parameter*/) {
Table pointtable = malloc(sizeof(struct table));
return pointtable;
}
////


void insertnew(tree_ptr node, Key_Type element)
{
    Key_Type current = node->element;
    if(strcmp(element,current) < 0)//left
    {
      if(node -> left == NULL)
      {
        typedef struct node *pointsobject;
        //new sobject 
    pointsobject structsobject;
    // memory alocated
    structsobject = malloc(sizeof(*structsobject));
    structsobject-> element = element;
    structsobject-> left = NULL;
    structsobject-> right = NULL;
    node->left = structsobject;
      }
      else
        insertnew(node->left , element);
      }
      else if(strcmp(element,current) > 0)//right
      {
      if(node -> right == NULL)
      {
        typedef struct node *pointsobject;
        //new sobject 
    pointsobject structsobject;
    // memory alocated
    structsobject = malloc(sizeof(*structsobject));
    structsobject-> element = element;
    structsobject-> left = NULL;
    structsobject-> right = NULL;
    node->right = structsobject;
      }
      else
        insertnew(node->right, element);
      }

}



Table insert(Key_Type element,Table dictable) {
  if (dictable -> head == NULL)
  { 
   typedef struct node *pointsobject;
   //new sobject 
   pointsobject structsobject;
   // memory alocated
   structsobject = malloc(sizeof(*structsobject));
   structsobject-> element = element;
   structsobject-> left = NULL;
   structsobject-> right = NULL;
   dictable->head = structsobject;
  } 
  else
  {
    insertnew(dictable->head,element);
  }
  return dictable;
}

Boolean find(Key_Type element, Table dictable)  {
return FALSE;;
}


void printtab(tree_ptr node)
{
  if (node->left != NULL)
    printtab(node->left);
  if (node->right != NULL)
    printtab(node->right);
  printf("%s",node->element);
}

void print_table(Table dictable) {
printtab(dictable->head);
}


void print_stats (Table dictable) {
}

我试图在网上寻找实现,但发现解释相当混乱。 有人能指出我如何将此树结构更改为 avl 树结构的正确方向吗?

谢谢

i am currently learning c and algorithms.
I have implemented a mock up dictionary as a tree and would like to take my course work a bit further and use an avl tree.

  #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define __USE_BSD
#include <string.h>

#include "speller.h"
#include "dict.h"

typedef struct node * tree_ptr;
struct node {
  Key_Type element; // only data is the key itself
  tree_ptr left, right;
  // add anything else that you need
};

struct table {
  tree_ptr head; // points to the head of the tree
  // add anything else that you need
}dictable;

Table initialize_table(/*ignore parameter*/) {
Table pointtable = malloc(sizeof(struct table));
return pointtable;
}
////


void insertnew(tree_ptr node, Key_Type element)
{
    Key_Type current = node->element;
    if(strcmp(element,current) < 0)//left
    {
      if(node -> left == NULL)
      {
        typedef struct node *pointsobject;
        //new sobject 
    pointsobject structsobject;
    // memory alocated
    structsobject = malloc(sizeof(*structsobject));
    structsobject-> element = element;
    structsobject-> left = NULL;
    structsobject-> right = NULL;
    node->left = structsobject;
      }
      else
        insertnew(node->left , element);
      }
      else if(strcmp(element,current) > 0)//right
      {
      if(node -> right == NULL)
      {
        typedef struct node *pointsobject;
        //new sobject 
    pointsobject structsobject;
    // memory alocated
    structsobject = malloc(sizeof(*structsobject));
    structsobject-> element = element;
    structsobject-> left = NULL;
    structsobject-> right = NULL;
    node->right = structsobject;
      }
      else
        insertnew(node->right, element);
      }

}



Table insert(Key_Type element,Table dictable) {
  if (dictable -> head == NULL)
  { 
   typedef struct node *pointsobject;
   //new sobject 
   pointsobject structsobject;
   // memory alocated
   structsobject = malloc(sizeof(*structsobject));
   structsobject-> element = element;
   structsobject-> left = NULL;
   structsobject-> right = NULL;
   dictable->head = structsobject;
  } 
  else
  {
    insertnew(dictable->head,element);
  }
  return dictable;
}

Boolean find(Key_Type element, Table dictable)  {
return FALSE;;
}


void printtab(tree_ptr node)
{
  if (node->left != NULL)
    printtab(node->left);
  if (node->right != NULL)
    printtab(node->right);
  printf("%s",node->element);
}

void print_table(Table dictable) {
printtab(dictable->head);
}


void print_stats (Table dictable) {
}

i have tried to look for an implementation online and find the explanations fairly confusing.
could someone point me in the right direction on how to change this tree structure into an avl tree structure.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-10-24 12:28:47

您可以首先在此处获取一些想法

,我认为wikipedia 对 avl 树解释得足够清楚

You can start by grabbing some ideas here

and I think wikipedia explains clearly enough about avl tree

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文