用C N元树 [英] N-ary trees in C

查看:178
本文介绍了用C N元树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个N叉树整齐实行C语言?

格外,我想实现一个n叉树,不能自行ballancing,带着孩子在每个节点的绑定号码,其中每个节点拥有一个已定义的结构,像这样的例子:

 结构的任务{
  字符命令[MAX_LENGTH]
  INT required_time;
};


解决方案

在第一遍,你可以简单地创建的结构的(我们称之为的树节点的)持有一个的任务的,以及一组指针来的树节点的秒。这组既可以是一个数组(如 N 是固定的),或者一个链表(如 N 是可变的)。链表需要你声明的额外的结构的(让我们把它叫做的 ListNode 的)用的树节点的指针实际子(的一部分树),和一个指向下的 ListNode 的列表(无效如果在列表的末尾)。

这可能是这个样子:

 结构的任务{
  字符命令[MAX_LENGTH]
  INT required_time;
};结构树节点;结构ListNode {
  结构树节点*子女;
  结构ListNode *接下来的;
};结构树节点{
  结构任务myTask;
  结构ListNode myChildList;
};

Which would be a neat implemenation of a N-ary tree in C language?

Particulary, I want to implement an n-ary tree, not self-ballancing, with an unbound number of children in each node, in which each node holds an already defined struct, like this for example:

struct task {
  char command[MAX_LENGTH];
  int required_time;
};

解决方案

As a first pass, you could simply create a struct (let's call it TreeNode) which holds a task, as well as a set of pointers to TreeNodes. This set could either be an array (if N is fixed) or a linked list (if N is variable). The linked list would require you to declare an additional struct (let's called it ListNode) with a TreeNode pointer to the actual child (part of the tree), and a pointer to the next ListNode in the list (null if at the end of the list).

It might look something like this:

struct task {
  char command[MAX_LENGTH];
  int required_time;
};

struct TreeNode;

struct ListNode {
  struct TreeNode * child;
  struct ListNode * next;
};

struct TreeNode {
  struct task myTask;
  struct ListNode myChildList;
};

这篇关于用C N元树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆