Advertisement
Hazem3529

TREE

Jan 4th, 2016
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace tree
  7. {
  8.     class BStnode
  9.     {
  10.         public int Data;
  11.  
  12.         public BStnode left;
  13.  
  14.         public BStnode right;
  15.  
  16.  
  17.         public BStnode(int value)
  18.         {
  19.             Data = value;
  20.             right = left = null;
  21.  
  22.  
  23.         }
  24.  
  25.  
  26.         public void Printnode()
  27.         {
  28.  
  29.             Console.WriteLine(Data);
  30.         }
  31.  
  32.  
  33.         //insert
  34.  
  35.         public void insert(int value)
  36.         {
  37.             if (value < Data)
  38.             {
  39.                 if (left == null)
  40.                     left = new BStnode(value);
  41.  
  42.                 else
  43.                     left.insert(value);
  44.  
  45.             } //end if
  46.  
  47.  
  48.             else
  49.             {
  50.                 if (right == null)
  51.                     right = new BStnode(value);
  52.  
  53.                 else
  54.                     right.insert(value);
  55.  
  56.             } //end else
  57.  
  58.  
  59.         } //end insert
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.     }
  68.  
  69.     class Bsttree
  70.     {
  71.  
  72.       public  BStnode Root;
  73.  
  74.         public Bsttree()
  75.         {
  76.  
  77.             Root = null;
  78.         }
  79.  
  80.         public void insertnode(int value)
  81.         {
  82.             if (Root == null)
  83.                 Root = new BStnode(value);
  84.  
  85.             else
  86.                 Root.insert(value);
  87.  
  88.         }
  89.  
  90.  
  91.         public void preorderhelp(BStnode n)
  92.         {
  93.  
  94.             if (n == null)
  95.                 return;
  96.  
  97.  
  98.             Console.WriteLine(n.Data);
  99.  
  100.             preorderhelp(n.left);
  101.  
  102.             preorderhelp(n.right);
  103.  
  104.  
  105.  
  106.         }//end preorder
  107.  
  108.  
  109.  
  110.  
  111.  
  112.         public void inorderhelp(BStnode n)
  113.         {
  114.  
  115.             if (n == null)
  116.                 return;
  117.  
  118.  
  119.             inorderhelp (n.left);
  120.  
  121.             Console.WriteLine(n.Data);
  122.  
  123.  
  124.  
  125.                  inorderhelp(n.right);
  126.  
  127.  
  128.  
  129.         }//end inorder
  130.  
  131.  
  132.  
  133.         public void postrderhelp(BStnode n)
  134.         {
  135.  
  136.             if (n == null)
  137.                 return;
  138.  
  139.  
  140.             postrderhelp(n.left);
  141.  
  142.             postrderhelp(n.right);
  143.  
  144.             Console.WriteLine(n.Data);
  145.  
  146.  
  147.         } //end postorder
  148.  
  149.  
  150.  
  151.  
  152.         // to print from root in 3 methods
  153.  
  154.  
  155.  
  156.  
  157.  
  158.         public void preorder()
  159.         {
  160.  
  161.             preorderhelp(Root);
  162.  
  163.         }
  164.  
  165.  
  166.         public void inorder()
  167.         {
  168.  
  169.             inorderhelp(Root);
  170.  
  171.         }
  172.  
  173.         public void postorder()
  174.         {
  175.  
  176.             postrderhelp(Root);
  177.  
  178.         }
  179.  
  180.         public BStnode searchhelp(BStnode start, int key)
  181.         {
  182.             if (start == null)
  183.                 return null;
  184.  
  185.             else if (start.Data == key)
  186.                 return start;
  187.  
  188.             else if (start.Data > key)
  189.                 return searchhelp(start.left, key);
  190.  
  191.  
  192.             else
  193.                 return searchhelp(start.right, key);
  194.  
  195.         }
  196.  
  197.  
  198.     }
  199.     class Program
  200.     {
  201.         static void Main(string[] args)
  202.         {
  203.  
  204.             Bsttree mytree = new Bsttree();
  205.             mytree.insertnode(10);
  206.              mytree.insertnode(11);
  207.              mytree.insertnode(12);
  208.              mytree.insertnode(15);
  209.              mytree.insertnode(19);
  210.              mytree.insertnode(8);
  211.              mytree.insertnode(7);
  212.              mytree.insertnode(5);
  213.              mytree.insertnode(2);
  214.            Console.WriteLine("POST ORDR NLR");
  215.             mytree.postorder();
  216.             Console.WriteLine("_____________________");
  217.             mytree.preorder();
  218.             Console.WriteLine("_________________-");
  219.             mytree.inorder();
  220.             Console.WriteLine("_________________");
  221.             mytree.inorderhelp(mytree.searchhelp(mytree.Root, 8));
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.             }
  231.  
  232.  
  233.  
  234.         }
  235.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement