using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 树的孩子链表存储结构 { public class Node<T> { private T value; private ChildNode firstNode; public T Value { get { return this.value; } set { this.value = value; } } public ChildNode FirstNode { get { return this.firstNode; } set { this.firstNode = value; } } public Node() { this.value = default(T); this.firstNode = null; } public Node(T t, ChildNode node) { this.value = t; this.firstNode = node; } } public class ChildNode { private int index; private ChildNode nextNode; public int Index { get { return this.index; } set { this.index = value; } } public ChildNode NextNode { get { return this.nextNode; } set { this.nextNode = value; } } public ChildNode() { this.index = -1; this.nextNode = null; } public ChildNode(int i,ChildNode next) { this.index = i; this.nextNode = next; } public ChildNode(int i) { this.index = i; this.nextNode = null; } } public class TreeDS<T> { private Node<T>[] nodesInTree; public Node<T>[] NodesInTree { get { return this.nodesInTree; } set { this.nodesInTree = value; } } public TreeDS() { this.nodesInTree = new Node<T>[10];//默认带10个节点 } public TreeDS(int n) { this.nodesInTree = new Node<T>[n]; } //返回某结点的子节点 public List<Node<T>> GetChildNodes(Node<T> n) { List<Node<T>> list = new List<Node<T>>(); if (n.FirstNode != null) { ChildNode cn = n.FirstNode; Node<T> node = this.nodesInTree[cn.Index]; list.Add(node); cn = cn.NextNode; while (cn != null) { list.Add(this.nodesInTree[cn.Index]); cn = cn.NextNode; } return list; } else { return list; } } } }