Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.44 KB | None | 0 0
  1. using CustomTree.Interfaces;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace CustomTree
  10. {
  11.     public class BinarySearchTree : IBinarySearchTree, IEnumerable<TreeNode>, ICloneable
  12.     {
  13.         public TreeNode Root { get; set; }
  14.  
  15.         public BinarySearchTree(TreeNode root)
  16.         {
  17.             this.Root = root;
  18.         }
  19.  
  20.         public void Delete(TreeNode node)
  21.         {
  22.             if (this.Root == node)
  23.             {
  24.                 throw new InvalidOperationException("Cannot delete the root of the tree!");
  25.             }
  26.  
  27.             DeleteDFS(this.Root, node);
  28.         }
  29.  
  30.         //Separate depth first search method for finding and deleting the node we wish to remove
  31.         private void DeleteDFS(TreeNode treeNode, TreeNode nodeToRemove)
  32.         {
  33.             foreach (TreeNode currentNode in treeNode.LeftChildren)
  34.             {
  35.                 if (!currentNode.IsVisited)
  36.                 {
  37.                     if (currentNode == nodeToRemove)
  38.                     {
  39.                         treeNode.LeftChildren.Remove(currentNode);
  40.                         return;
  41.                     }
  42.  
  43.                     currentNode.IsVisited = true;
  44.                     DeleteDFS(currentNode, nodeToRemove);
  45.                 }
  46.             }
  47.  
  48.             foreach (TreeNode currentNode in treeNode.RightChildren)
  49.             {
  50.                 if (!currentNode.IsVisited)
  51.                 {
  52.                     if (currentNode == nodeToRemove)
  53.                     {
  54.                         treeNode.RightChildren.Remove(currentNode);
  55.                         return;
  56.                     }
  57.  
  58.                     currentNode.IsVisited = true;
  59.                     DeleteDFS(currentNode, nodeToRemove);
  60.                 }
  61.             }
  62.         }
  63.  
  64.         public TreeNode Search(int nodeToBeFound)
  65.         {
  66.             if (this.Root.Value == nodeToBeFound)
  67.             {
  68.                 return this.Root;
  69.             }
  70.  
  71.             return SearchDFS(this.Root, nodeToBeFound);
  72.         }
  73.  
  74.         //Separate depth first search algrith purposed for searching for a specific node in the binary tree
  75.         private TreeNode SearchDFS(TreeNode treeNode, int nodeToBeFound)
  76.         {
  77.             foreach (TreeNode currentNode in treeNode.LeftChildren)
  78.             {
  79.                 if (currentNode.Value == nodeToBeFound)
  80.                 {
  81.                     return currentNode;
  82.                 }
  83.  
  84.                 SearchDFS(currentNode, nodeToBeFound);
  85.             }
  86.  
  87.             foreach (TreeNode currentNode in treeNode.RightChildren)
  88.             {
  89.                 if (currentNode.Value == nodeToBeFound)
  90.                 {
  91.                     return currentNode;
  92.                 }
  93.  
  94.                 SearchDFS(currentNode, nodeToBeFound);
  95.             }
  96.  
  97.             return null;
  98.         }
  99.  
  100.         public IEnumerator<TreeNode> GetEnumerator()
  101.         {
  102.             //All nodes in the binary search tree are added to a separate list. That list we can then iterate through
  103.             List<TreeNode> nodesData = new List<TreeNode>();
  104.             nodesData.Add(this.Root);
  105.  
  106.             PrintDFS(this.Root, nodesData);
  107.  
  108.             for (int i = 0; i < nodesData.Count; i++)
  109.             {
  110.                 yield return nodesData[i];
  111.             }
  112.         }
  113.  
  114.         private void PrintDFS(TreeNode currentNode, List<TreeNode> nodesData)
  115.         {
  116.             foreach (TreeNode childNode in currentNode.LeftChildren)
  117.             {
  118.                 nodesData.Add(childNode);
  119.                 PrintDFS(childNode, nodesData);
  120.             }
  121.  
  122.             foreach (TreeNode childNode in currentNode.RightChildren)
  123.             {
  124.                 nodesData.Add(childNode);
  125.                 PrintDFS(childNode, nodesData);
  126.             }
  127.         }
  128.  
  129.         IEnumerator IEnumerable.GetEnumerator()
  130.         {
  131.             return this.GetEnumerator();
  132.         }
  133.  
  134.         public object Clone()
  135.         {
  136.             //Here by we can clone the whole tree by just cloning the root where the whole tree is implemented
  137.             //TreeNode clonedRoot = this.Root.Clone() as TreeNode;
  138.             //BinarySearchTree clonedTree = new BinarySearchTree(clonedRoot);
  139.             //return clonedTree;
  140.  
  141.             //With the method below we can reassemble the cloned tree by using the given root of the original tree
  142.             //by using the breath first search algorithm
  143.             BinarySearchTree assembledTree = AssembleTree(this.Root);
  144.             return assembledTree;
  145.         }
  146.  
  147.         private BinarySearchTree AssembleTree(TreeNode rootNode)
  148.         {
  149.             BinarySearchTree clonedTree = new BinarySearchTree(new TreeNode(rootNode.Value));
  150.  
  151.             Queue<TreeNode> nodeQueue = new Queue<TreeNode>();
  152.             nodeQueue.Enqueue(rootNode);
  153.  
  154.             while (nodeQueue.Count != 0)
  155.             {
  156.                 TreeNode originalNode = nodeQueue.Dequeue();
  157.                 TreeNode clonedNode = clonedTree.Search(originalNode.Value);
  158.  
  159.                 foreach (TreeNode childNode in originalNode.LeftChildren)
  160.                 {
  161.                     clonedNode.LeftChildren.Add(new TreeNode(childNode.Value));
  162.                     nodeQueue.Enqueue(childNode);
  163.                 }
  164.  
  165.                 foreach (TreeNode childNode in originalNode.RightChildren)
  166.                 {
  167.                     clonedNode.RightChildren.Add(new TreeNode(childNode.Value));
  168.                     nodeQueue.Enqueue(childNode);
  169.                 }
  170.             }
  171.  
  172.             return clonedTree;
  173.         }
  174.  
  175.         public void Add(TreeNode node)
  176.         {
  177.             throw new NotImplementedException();
  178.         }
  179.  
  180.         public override string ToString()
  181.         {
  182.             StringBuilder sb = new StringBuilder();
  183.  
  184.             StringBuilderDFS(this.Root, sb, 0);
  185.  
  186.             return sb.ToString();
  187.         }
  188.  
  189.         private void StringBuilderDFS(TreeNode rootNode, StringBuilder sb, int index)
  190.         {
  191.             sb.AppendLine(string.Format("{0}-> {1}", new string(' ', index), rootNode.Value));
  192.  
  193.             foreach (TreeNode childNode in rootNode.LeftChildren)
  194.             {
  195.                 StringBuilderDFS(childNode, sb, index + 2);
  196.             }
  197.  
  198.             foreach (TreeNode childNode in rootNode.RightChildren)
  199.             {
  200.                 StringBuilderDFS(childNode, sb, index + 2);
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement