二叉树;按频率递增顺序打印; C语言

时间:2014-12-01 00:32:21

标签: c binary-tree

我应该使用我所知道的,这是课堂作业,我只能在K& R书中第7章

我想要做的是遍历已经创建的二叉树(按字典顺序排列)并按照频率递增的顺序打印结构,这是我的递归树形图。我递归遍历树形图的想法是检查pcount与treenode计数,如果它们是相同的打印节点,将其计数设置为-4,使其超出打印(numcount现在不再使用)所以当它得到一个空指针或p->计数等于-4,这应该意味着树已经完全遍历,我们应该重新开始并增加pcount以检查该频率。我知道有些事情是错的,但我无法弄清楚。我使用了一个名为gettysburg的文本文件,我已经将文本包含在最后

 /* treeprint: in-order print of tree p */
  void treeprint(struct tnode *p)
  {

      if(p != NULL) {
          treeprint(p->left);
          if(p->count == -4 || p == NULL){
              pcount++;
              treeprint(pt);
          }
          if(pcount == p->count) {
          printf("%4d %s\n", p->count, p->word);
          p->count = -4;
          numcount++;
      //  treeprint(p->right);
          }
          treeprint(p->right);
      }
  }

这里是声明和主体:

  #include <stdio.h>
  #include <ctype.h>
  #include <string.h>
  #include <stdlib.h>
  #include "getch.h"
  #define MAXWORD 100

  int numcount = 1;
  int pcount = 1;
  struct tnode * pt;

  typedef struct tnode{
      char *word;
      int count;
      struct tnode *left;
      struct tnode *right;
  }treenode;


  int getword(char *, int);
  struct tnode *addtree(struct tnode *, char *);
  void treeprint(struct tnode *);
  int getch(void);
  void ungetch(int);
  main()
  {
      struct tnode *root;
      struct tnode *sortedtree;
      char word[MAXWORD];

      root = NULL;
      while(getword(word, MAXWORD) != EOF)
          if(isalpha(word[0])){
              root = addtree(root, word);
              pt = root;
          }
  //  sortedtree = treesort(sortedtree, root);
      treeprint(root);
  //  printf("so far so good \n\n");
  //  treeprint(sortedtree);
      printf("numcount = %d\n", numcount);
      return 0;
  }

我的构建树的addtree函数:

  /* addtree: add a node with w, at or below p*/
  struct tnode *addtree(struct tnode *p, char *w)
  {
      int cond;

      if(p == NULL) {
          p = (struct tnode *) malloc(sizeof(struct tnode));
          p->word = strdup(w);
          p->count = 1;
          p->left = p->right = NULL;
      } else if ((cond = strcmp(w, p->word)) == 0)
          p->count++;
      else if (p->count < 0)
          p->left = addtree(p->left, w);
      else
          p->right = addtree(p->right, w);
      return p;
  }

葛底斯堡文:

  

四年前和七年前,我们的父亲提出了这个问题   大陆是一个新的国家,在自由中孕育,并致力于   所有人都是平等的主张。现在我们正在搞一个   伟大的内战,测试那个国家或任何国家是如此   构思和如此专注,可以长久忍受。我们遇到了一个伟大的   那场战争的战场。我们已经致力于其中的一部分   田野,作为在这里献出生命的人的最后安息之所   那个国家可能会生活。这完全合适   我们应该这样做。但是,从更广泛的意义上说,我们不能奉献   没有奉献,我们不能让这个理由成为现实。勇敢的人,活着   在这里挣扎的死者已经将它奉献给了我们,远远超过了我们的穷人   增加或减少的力量。世界将很少注意到,也不会长久记住   我们在这里说的是什么,但它永远不会忘记他们在这里做了什么。它是   对我们来说,生活,而是在这里献给未完成的工作   到目前为止,他们在这里战斗的人如此高尚。它是   而是让我们在这里致力于以前的伟大任务   我们从这些荣幸的死者中我们更加热爱这一点   因为他们给了我们最后一个充分的奉献精神   在这里高度决心,这些死者不会白白死去   在上帝的统治下,这个国家将重新获得自由   人民政府,人民政府,人民政府,不应该   从地球上消失。

1 个答案:

答案 0 :(得分:0)

错误的部分原因是您没有提及将其按频率顺序排序的方法。

编写堆结构,遍历单词树并插入每个单词会更容易:计入堆中,然后从堆中删除并打印值。

堆是部分有序的树,其最大值始终位于根。在插入和删除时,树被重新平衡以维护此属性。有一个非常有效的实现,它将堆映射到一个数组,s.t。 root位于偏移量1,左侧子节点位于偏移量2 *根处,右侧子节点位于2 * root + 1处。堆是一种有趣的结构。