Advertisement
sylviapsh

Tree and Traversal (demo)

Jun 23rd, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class TreeNode<T>
  5. {
  6.     private T value;
  7.     private bool hasParent;
  8.     private List<TreeNode<T>> children;
  9.  
  10.     public TreeNode(T value)
  11.     {
  12.         if (value == null)
  13.         {
  14.             throw new ArgumentNullException(
  15.                 "Cannot insert null value!");
  16.         }
  17.         this.value = value;
  18.         this.children = new List<TreeNode<T>>();
  19.     }
  20.  
  21.     public T Value
  22.     {
  23.         get
  24.         {
  25.             return this.value;
  26.         }
  27.         set
  28.         {
  29.             this.value = value;
  30.         }
  31.     }
  32.  
  33.     public int ChildrenCount
  34.     {
  35.         get
  36.         {
  37.             return this.children.Count;
  38.         }
  39.     }
  40.  
  41.     public void AddChild(TreeNode<T> child)
  42.     {
  43.         if (child == null)
  44.         {
  45.             throw new ArgumentNullException(
  46.                 "Cannot insert null value!");
  47.         }
  48.  
  49.         if (child.hasParent)
  50.         {
  51.             throw new ArgumentException(
  52.                 "The node already has a parent!");
  53.         }
  54.  
  55.         child.hasParent = true;
  56.         this.children.Add(child);
  57.     }
  58.  
  59.     public TreeNode<T> GetChild(int index)
  60.     {
  61.         return this.children[index];
  62.     }
  63. }
  64.  
  65. public class Tree<T>
  66. {
  67.     private TreeNode<T> root;
  68.  
  69.     public Tree(T value)
  70.     {
  71.         if (value == null)
  72.         {
  73.             throw new ArgumentNullException(
  74.                 "Cannot insert null value!");
  75.         }
  76.  
  77.         this.root = new TreeNode<T>(value);
  78.     }
  79.  
  80.     public Tree(T value, params Tree<T>[] children) : this(value)
  81.     {
  82.         foreach (Tree<T> child in children)
  83.         {
  84.             this.root.AddChild(child.root);
  85.         }
  86.     }
  87.  
  88.     /// <summary>
  89.     /// The root node or null if the tree is empty
  90.     /// </summary>
  91.     public TreeNode<T> Root
  92.     {
  93.         get
  94.         {
  95.             return this.root;
  96.         }
  97.     }
  98.  
  99.     private void TraverseDFS(TreeNode<T> root, string spaces)
  100.     {
  101.         if (this.root == null)
  102.         {
  103.             return;
  104.         }
  105.  
  106.         Console.WriteLine(spaces + root.Value);
  107.  
  108.         TreeNode<T> child = null;
  109.         for (int i = 0; i < root.ChildrenCount; i++)
  110.         {
  111.             child = root.GetChild(i);
  112.             TraverseDFS(child, spaces + "   ");
  113.         }
  114.     }
  115.  
  116.     public void TraverseDFS()
  117.     {
  118.         this.TraverseDFS(this.root, string.Empty);
  119.     }
  120.  
  121.     public void TraverseBFS()
  122.     {
  123.         Queue<TreeNode<T>> queue = new Queue<TreeNode<T>>();
  124.         queue.Enqueue(this.root);
  125.         while (queue.Count > 0)
  126.         {
  127.             TreeNode<T> currentNode = queue.Dequeue();
  128.             Console.Write("{0} ", currentNode.Value);
  129.             for (int i = 0; i < currentNode.ChildrenCount; i++)
  130.             {
  131.                 TreeNode<T> childNode = currentNode.GetChild(i);
  132.                 queue.Enqueue(childNode);
  133.             }
  134.         }
  135.     }
  136.  
  137.     public void TraverseDFSWithStack()
  138.     {
  139.         Stack<TreeNode<T>> stack = new Stack<TreeNode<T>>();
  140.         stack.Push(this.root);
  141.         while (stack.Count > 0)
  142.         {
  143.             TreeNode<T> currentNode = stack.Pop();
  144.             Console.Write("{0} ", currentNode.Value);
  145.             for (int i = 0; i < currentNode.ChildrenCount; i++)
  146.             {
  147.                 TreeNode<T> childNode = currentNode.GetChild(i);
  148.                 stack.Push(childNode);
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. public static class TreeExample
  155. {
  156.     static void Main()
  157.     {
  158.         Tree<int> tree =
  159.         new Tree<int>(7,
  160.                       new Tree<int>(19,
  161.                                     new Tree<int>(1),
  162.                                     new Tree<int>(12),
  163.                                     new Tree<int>(31)),
  164.                       new Tree<int>(21),
  165.                       new Tree<int>(14,
  166.                                     new Tree<int>(23),
  167.                                     new Tree<int>(6)));
  168.  
  169.         Console.WriteLine("Depth-First Search (DFS) traversal (recursive):");
  170.         tree.TraverseDFS();
  171.         Console.WriteLine();
  172.  
  173.         Console.WriteLine("Breadth-First Search (BFS) traversal (with queue):");
  174.         tree.TraverseBFS();
  175.         Console.WriteLine();
  176.  
  177.         Console.WriteLine();
  178.         Console.WriteLine("Depth-First Search (DFS) traversal (with stack):");
  179.         tree.TraverseDFSWithStack();
  180.         Console.WriteLine();
  181.     }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement