C # tree type and its Traversal

Source: Internet
Author: User

C # tree type and its Traversal
Recently, a project requires not only the hierarchical relationship of a department, but also the irregular relationship (removing a department). Only the tree structure can achieve relevant traversal and operations. Knowledge points involved: generics, recursion, and data structures. Since the tree type is studied, Let's first look at the definition of the next tree: A tree is composed of n (n> 0) A finite set of elements. (1) Each element is called a node. (2) A specific node is called a root node or a root node ); (3) Except for the root node, the other nodes are divided into m (m> = 0) finite sets of non-Intersecting, and each subset is a tree (called the subtree of the original tree ); -- Baidu this article will simplify the tree and only study the node-node tree of the tree. The node tree contains: parent node (the root node's parent node is null), child node (List set), and data object. Class Design:

Public class BoTree <T> {public BoTree () {nodes = new List <BoTree <T> ();} public BoTree (T data) {this. data = data; nodes = new List <BoTree <T> ();} private BoTree <T> parent; /// <summary >/// Parent node /// </summary> public BoTree <T> parent {get {return Parent ;}} /// <summary> // node Data // </summary> public T Data {get; set;} private List <BoTree <T> nodes; /// <summary> /// sub-node /// </summary> pu Blic List <BoTree <T> Nodes {get {return nodes ;}} /// <summary> /// Add a node /// </summary> /// <param name = "node"> node </param> public void AddNode (BoTree <t> node) {if (! Nodes. contains (node) {node. parent = this; nodes. add (node );}} /// <summary> /// Add a node /// </summary> /// <param name = "nodes"> node set </param> public void AddNode (List <BoTree <T> nodes) {foreach (var node in nodes) {if (! Nodes. contains (node) {node. parent = this; nodes. add (node );}}} /// <summary> /// Remove a node /// </summary> /// <param name = "node"> </param> public void Remove (BoTree <T> node) {if (nodes. contains (node) nodes. remove (node) ;}/// <summary> // clear the node set /// </summary> public void RemoveAll () {nodes. clear ();}}

 

Test: first create a student class (any)
public class Student    {        public Student(string name, string sex, int age)        {            this.Name = name;            this.Sex = sex;            this.Age = age;        }        public string Name { get; set; }        public string Sex { get; set; }        public int Age { get; set; }    }

 

Initialization tree:
BoTree <Student> tree1 = new BoTree <Student> (); tree1.Data = new Student ("wavelet 1", "male", 18 ); boTree <Student> tree2 = new BoTree <Student> (); tree2.Data = new Student ("wavelet 2", "male", 19 ); boTree <Student> tree3 = new BoTree <Student> (); tree3.Data = new Student ("wavelet 3", "male", 20 ); boTree <Student> tree4 = new BoTree <Student> (); tree4.Data = new Student ("wavelet 4", "male", 21); tree1.AddNode (tree2 ); tree1.AddNode (tree3); tree3.AddNode (tree4 );

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.