Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace TreeAndGraphStructures
  8. {
  9.     public class Tree<T>
  10.     {
  11.         private Node<T> root;
  12.  
  13.         public Tree(T value)
  14.         {
  15.             if (value == null)
  16.             {
  17.                 throw new ArgumentNullException(
  18.                     "Cannot have null value root");
  19.             }
  20.  
  21.             this.root = new Node<T>(value);
  22.         }
  23.  
  24.         public Tree(T value,params Tree<T>[] children)
  25.             : this(value)
  26.         {
  27.             foreach (Tree<T> child in children)
  28.             {
  29.                 root.Children.Add(child.root);
  30.             }
  31.         }
  32.  
  33.         public Node<T> Root
  34.         {
  35.             get
  36.             {
  37.                 return this.root;
  38.             }
  39.         }
  40.  
  41.         public void TraverseDFS(Node<T> root, string spaces)
  42.         {
  43.             if (root == null)
  44.             {
  45.                 return;
  46.             }
  47.  
  48.             Console.WriteLine(spaces + root.Value);
  49.  
  50.             foreach (Node<T> child in root.Children)
  51.             {
  52.                 TraverseDFS(child, spaces + "   ");
  53.             }
  54.         }
  55.  
  56.         public void TraverseDFS()
  57.         {
  58.             this.TraverseDFS(this.root, String.Empty);
  59.         }
  60.  
  61.         public void TraverseBFS()
  62.         {
  63.             Queue<Node<T>> queue = new Queue<Node<T>>();
  64.             queue.Enqueue(root);
  65.             string spaces = String.Empty;
  66.             int lastQueueSize = queue.Count;
  67.             while (queue.Count > 0)
  68.             {
  69.                 for (int i = 0; i < lastQueueSize; i++)
  70.                 {
  71.                     Node<T> top = queue.Dequeue();
  72.                     Console.WriteLine(spaces + top.Value);
  73.                     for (int j = 0; j < top.ChildrenCount; j++)
  74.                     {
  75.                         queue.Enqueue(top.Children[j]);
  76.                     }
  77.                    
  78.                 }
  79.                 lastQueueSize = queue.Count;
  80.                 spaces = spaces + "  ";
  81.             }
  82.  
  83.             return;
  84.         }
  85.  
  86.         public void TraverseDFSWithStack()
  87.         {
  88.             Stack<Node<T>> stack = new Stack<Node<T>>();
  89.             stack.Push(root);
  90.             while (stack.Count > 0)
  91.             {
  92.                 Node<T> top = stack.Pop();
  93.                 Console.WriteLine(top.Value);
  94.                 foreach (Node<T> child in top.Children)
  95.                 {
  96.                     stack.Push(child);
  97.                 }
  98.             }
  99.         }
  100.  
  101.  
  102.         //---------------------testing serialization-----------------------//
  103.  
  104.  
  105.         public void DumpBinaryTree()
  106.         {
  107.             const string filePath = @"../../BinaryTree.txt";
  108.             StreamWriter writter = new StreamWriter(filePath);
  109.             using (writter)
  110.             {
  111.                 RecursiveWrite(writter);
  112.             }
  113.            
  114.         }
  115.  
  116.  
  117.         public void RecursiveWrite(StreamWriter writter, Node<T> node)
  118.         {
  119.             if (node == null)
  120.             {
  121.                 writter.Write("-1 ");
  122.                 return;
  123.             }
  124.  
  125.             string data = node.Value.ToString();
  126.             int charsData = data.Length;
  127.             writter.Write(charsData + " " + data + " ");
  128.  
  129.             for (int i = 0; i < node.ChildrenCount; i++)
  130.             {
  131.                 RecursiveWrite(writter, node.Children[i]);
  132.             }
  133.  
  134.             for (int i = node.ChildrenCount; i < 2; i++)
  135.             {
  136.                 RecursiveWrite(writter, null);
  137.             }
  138.         }
  139.  
  140.  
  141.         public void RecursiveWrite(StreamWriter writter)
  142.         {
  143.             RecursiveWrite(writter, this.root);
  144.         }
  145.  
  146.         public Tree<string> LoadBinaryTree()
  147.         {
  148.             const string filePath = @"../../BinaryTree.txt";
  149.             StreamReader reader = new StreamReader(filePath);
  150.             Tree<string> newTree = null;
  151.  
  152.             using (reader)
  153.             {
  154.                 string line = reader.ReadLine();
  155.                 string[] treeParts = line.Split();
  156.  
  157.                 int now = 0;
  158.                 newTree = MakeTree(treeParts,ref now);
  159.             }
  160.             return newTree;
  161.         }
  162.  
  163.         private Tree<string> MakeTree(string[] treeParts,ref int now)
  164.         {
  165.             if (treeParts[now] == "-1")
  166.             {
  167.                
  168.                 return null;
  169.             }
  170.  
  171.             now++;
  172.             Tree<string> tree = new Tree<string>(treeParts[now]);
  173.  
  174.             now++;
  175.             Tree<string> leftSubtree = MakeTree(treeParts, ref now);
  176.            
  177.             now++;
  178.             Tree<string> rightSubtree = MakeTree(treeParts, ref now);
  179.  
  180.             if (leftSubtree != null)
  181.             {
  182.                 tree.root.AddChild(leftSubtree.root);
  183.             }
  184.             if (rightSubtree != null)
  185.             {
  186.                 tree.root.AddChild(rightSubtree.root);
  187.             }
  188.  
  189.             return tree;
  190.         }
  191.  
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement