gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:04 26 4
gpt4 key购买 nike

我应该使用我所知道的,这是类作业,我只读到 K&R 书中的第 7 章

我想做的是遍历一个已经创建的二叉树(按字典顺序排列)并按频率递增的顺序打印结构,这是我的递归 TreeMap 。我递归遍历 treeprint 的想法是检查 pcount 与 treenode 计数,如果它们相同则打印节点,将其计数设置为 -4 以便它停止打印(numcount 现在不使用)所以当它得到一个空指针或者 p->count 等于 -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;
}

葛底斯堡文本:

Four score and seven years ago our fathers brought forth on this continents a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated, can long endure. We are met on a great battle field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate we can not consecrate we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion that we here highly resolve that these dead shall not have died in vain that this nation, under God, shall have a new birth of freedom and that government of the people, by the people, for the people, shall not perish from the earth.

最佳答案

部分问题在于您没有提及将它们按频率排序的方法。

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

堆是一棵偏序树,最大值总是在根部。在插入和删除时,树会重新平衡以维护此属性。有一个非常有效的实现,可以将堆映射到数组 s.t.根在偏移量 1 处,左 child 在偏移量 2*root 处,右 child 在偏移量 2*root + 1 处。堆是一种编写起来很有趣的结构。

关于c - 二叉树;按频率递增的顺序打印; C语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27220097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com