Binary Tree (simple version), binary tree

Source: Internet
Author: User

Binary Tree (simple version), binary tree

A tree is a non-linear structure. The essence of a tree is to connect some nodes by edge to form a hierarchical structure, that is, the relationship between 1: N. The following is a manual relationship between data:
Using System; using System. collections. generic; using System. linq; using System. text; namespace Tree3 {// manually build the relationship between nodes public class Program {static void Main (string [] args) {Node <string> rootNode = BinTree (); Console. writeLine ("first-order traversal method traversing a binary tree:"); PreOrde (rootNode); Console. writeLine (""); Console. writeLine (""); Console. writeLine ("traversing a binary tree using a forward Traversal method:"); InOrde (rootNode); Console. writeLine (""); Console. writeLine ("" ); Console. writeLine ("post-order traversal method traversing Binary Tree:"); AfterOrde (rootNode); Console. readKey ();} /// <summary> /// construct a binary tree /// </summary> /// <returns> </returns> public static Node <string> BinTree () {Node <string> [] binTree = new Node <string> [11]; // create A Node binTree [0] = new Node <string> (""); binTree [1] = new Node <string> ("B"); binTree [2] = new Node <string> ("C "); binTree [3] = new Node <string> ("D"); binTree [4] = new Nod E <string> ("E"); binTree [5] = new Node <string> ("F "); binTree [6] = new Node <string> ("G"); binTree [7] = new Node <string> ("H "); binTree [8] = new Node <string> ("J"); binTree [9] = new Node <string> ("K "); binTree [10] = new Node <string> ("L"); // construct a link binTree [0]. LNode = binTree [1]; binTree [0]. RNode = binTree [2]; binTree [1]. LNode = binTree [3]; binTree [1]. RNode = binTree [4]; binTree [2]. LNode = binTree [6]; binTr Ee [2]. RNode = binTree [7]; binTree [6]. RNode = binTree [8]; binTree [7]. RNode = binTree [9]; binTree [8]. RNode = binTree [10]; // return the return binTree with the node [0];} /// <summary> /// first traverse (first access to the node-> access the left child-> access the right child) recursion // note that: the middle-order traversal method is still used to traverse left and right subtree. /// </Summary> /// <typeparam name = "T"> </typeparam> /// <param name = "rootNode"> </param> public static void preOrde <T> (Node <T> rootNode) {if (rootNode! = Null) {Console. write (string. format ("{0}", rootNode. data); PreOrde (rootNode. LNode); PreOrde (rootNode. RNode); }}/// <summary> // traverse in the middle order (first access the left node-> access the right child) recursion /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "rootNode"> </param> public static void InOrde <T> (Node <T> rootNode) {if (rootNode! = Null) {InOrde (rootNode. LNode); Console. write (string. format ("{0}", rootNode. data); InOrde (rootNode. RNode); }}/// <summary> // traverse in the descending order (first access the left node-> access the right node-> access the child node) recursion /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "rootNode"> </param> public static void AfterOrde <T> (Node <T> rootNode) {if (rootNode! = Null) {AfterOrde (rootNode. LNode); AfterOrde (rootNode. RNode); Console. write (string. format ("{0}", rootNode. data) ;}}// Node class public class Node <T> {private T data; /// <summary >/// Data /// </summary> public T data {get {return data ;}set {Data = value ;}} private Node <T> lnode; // <summary> // left child // </summary> public Node <T> LNode {get {return lnode ;} set {lnode = value ;}} private Node <T> rnode; /// <summary> /// right child /// </summary> public Node <T> RNode {get {return rnode;} set {rnode = value ;}} /// <summary> /// No parameter constructor /// </summary> public Node () {}/// <summary> /// Node constructor /// </summary> /// <param name = "data"> </param> public Node (T data) {this. data = data ;}}}

Running result:


 

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.