下面是密码,
/* tree.c */
#include"tree.h"
int main(){
Tree *rootedTree = newTree();
insert(rootedTree, "~jrs/61b");
preOrder(rootedTree);
printf("\n");
}/* tree.h */
#include<stddef.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct SiblingTreeNode{
struct SiblingTreeNode *parent;
void *item;
struct SiblingTreeNode *firstChild;
struct SiblingTreeNode *nextSibling;
}Node;
typedef struct LCRSTree{
Node *root;
int size;
}Tree;
Tree * newTree(void){
Tree *rootedTree = malloc(sizeof(Tree));
rootedTree->root = NULL;
rootedTree->size = 0;
return rootedTree;
}
void insert(Tree *rootedTree, const void *item){
if(rootedTree->root == NULL){
Node *rootNode = malloc(sizeof(Node));
rootNode->parent = NULL;
rootNode->item = malloc(sizeof(strlen((const char *)item) + 1));
strcpy((char *)rootNode->item, (const char *)item);
rootNode->firstChild = NULL;
rootNode->nextSibling = NULL;
rootedTree->root = rootNode;
rootedTree->size = 0;
}else{
/*
Who takes care of the ordering of inserting items?
User or insert() function?
*/
}
}
void visit(Node *node){
printf("\n(%s)", (char *)(node->item));
}
/*
Pre-order traversal.
Visit each node before recursively visiting its children, left to right.
Root visited first
*/
void preOrderTraverse(Node * node){
visit(node);
if(node->firstChild){
printf("\n|");
preOrderTraverse(node->firstChild);
}
if(node->nextSibling){
printf("-->");
preOrderTraverse(node->nextSibling);
}
}
void preOrder(Tree *tree){
preOrderTraverse(tree->root);
}对于使用LCRS表示的根树。
使用Tree抽象,如果我需要构建一棵树,如下所示,在这里维护了cs61b课程的详细信息,

我下面的问题是,如何在根树中insert项?
以insert()函数为参数,以Tree和item为参数,易于(明显)实现insert()项"~jrs/61b"。
insert()ing firstChild和nextSibling项目在rootedTree中的顺序是什么?insert()的两个给定参数是否足够?
例如,我需要在insert()、"hw1"和"hw2"之后使用"hw"吗?还是我需要insert(),"index.html"在insert()ing "hw"之后立即
或
我是否需要维护单独的函数来插入item作为下一个兄弟和第一个孩子?
注意:不是要求代码。学习树
发布于 2018-10-26 14:07:32
以
insert()函数Tree和item为参数,…insert()的两个给定参数是否足够?
不,它们是不够的,因为树结构不能从insert()__ing的顺序推导出来。
我是否需要维护单独的函数来插入
item作为下一个兄弟和第一个孩子?
是的,或者类似的东西,比如告诉如何插入的参数。此外,需要将添加新项的节点作为参数。
https://stackoverflow.com/questions/41174542
复制相似问题