Tree with IComparable objects - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Tree

Description

Tree with IComparable objects

Demo Code

using System;/*from  ww w  . java  2 s .c o m*/
class TreeNode
{
   // automatic property LeftNode
   public TreeNode LeftNode { get; set; }
   public IComparable Data { get; private set; }
   public TreeNode RightNode { get; set; }
   public TreeNode(IComparable nodeData)
   {
      Data = nodeData;
   }
   public void Insert(IComparable insertValue)
   {
      if (insertValue.CompareTo(Data) < 0) // insert in left subtree
      {
         if (LeftNode == null)
         {
            LeftNode = new TreeNode(insertValue);
         }
         else // continue traversing left subtree
         {
            LeftNode.Insert(insertValue);
         }
      }
      else if (insertValue.CompareTo(Data) > 0) // insert in right
      {
         // insert new TreeNode
         if (RightNode == null)
         {
            RightNode = new TreeNode(insertValue);
         }
         else // continue traversing right subtree
         {
            RightNode.Insert(insertValue);
         }
      }
   }
}
public class Tree
{
   private TreeNode root;
   public void InsertNode(IComparable insertValue)
   {
      if (root == null)
      {
         root = new TreeNode(insertValue);
      }
      else
      {
         root.Insert(insertValue);
      }
   }
   public void PreorderTraversal()
   {
      PreorderHelper(root);
   }
   private void PreorderHelper(TreeNode node)
   {
      if (node != null)
      {
         Console.Write($"{node.Data} ");
         PreorderHelper(node.LeftNode);
         PreorderHelper(node.RightNode);
      }
   }
   public void InorderTraversal()
   {
      InorderHelper(root);
   }
   private void InorderHelper(TreeNode node)
   {
      if (node != null)
      {
         InorderHelper(node.LeftNode);
         Console.Write($"{node.Data} ");
         InorderHelper(node.RightNode);
      }
   }
   public void PostorderTraversal()
   {
      PostorderHelper(root);
   }
   private void PostorderHelper(TreeNode node)
   {
      if (node != null)
      {
         PostorderHelper(node.LeftNode);
         PostorderHelper(node.RightNode);
         Console.Write($"{node.Data} ");
      }
   }
}
class TreeTest
{
   static void Main()
   {
      int[] intArray = {8, 21, 24, 23, 31, 17, 25, 64};
      double[] doubleArray = {8.8, 2.2, 4.4, 3.3, 1.1, 7.7, 5.5, 6.6};
      string[] stringArray ={"eight", "two", "four","three", "one", "seven", "five", "six"};
      // int Tree
      Tree intTree = new Tree();
      PopulateTree(intArray, intTree, nameof(intTree));
      TraverseTree(intTree, nameof(intTree));
      // double Tree
      Tree doubleTree = new Tree();
      PopulateTree(doubleArray, doubleTree, nameof(doubleTree));
      TraverseTree(doubleTree, nameof(doubleTree));
      // string Tree
      Tree stringTree = new Tree();
      PopulateTree(stringArray, stringTree, nameof(stringTree));
      TraverseTree(stringTree, nameof(stringTree));
   }
   private static void PopulateTree(Array array, Tree tree, string name)
   {
      Console.WriteLine($"\n\n\nInserting into {name}:");
      foreach (IComparable data in array)
      {
         Console.Write($"{data} ");
         tree.InsertNode(data);
      }
   }
   private static void TraverseTree(Tree tree, string treeType)
   {
      // perform preorder traversal of tree
      Console.WriteLine($"\n\nPreorder traversal of {treeType}");
      tree.PreorderTraversal();
      // perform inorder traversal of tree
      Console.WriteLine($"\n\nInorder traversal of {treeType}");
      tree.InorderTraversal();
      // perform postorder traversal of tree
      Console.WriteLine($"\n\nPostorder traversal of {treeType}");
      tree.PostorderTraversal();
   }
}

Result


Related Tutorials