Hyluss

BST Binary Tree

Mar 20th, 2017
266
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace BST_c_sharp
  7. {
  8.     public class Node
  9.     {
  10.         private int data;
  11.         private Node left, right;
  12.  
  13.         public Node(int data)
  14.         {
  15.             this.data = data;
  16.             this.left = null;
  17.             this.right = null;
  18.         }
  19.  
  20.         public void Add (int data)
  21.         {
  22.             if (data > this.data)
  23.             {
  24.                 if (this.right !=  null)
  25.                 {
  26.                     this.right.Add(data);                    
  27.                 }
  28.                 else
  29.                 {
  30.                     this.right = new Node (data);
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 if (this.left != null)
  36.                 {
  37.                     this.left.Add(data);
  38.                 }
  39.                 else
  40.                 {
  41.                     this.left = new Node (data);
  42.                 }
  43.             }
  44.         }
  45.  
  46.         public void Clear(Node root)
  47.         {
  48.                 if (root.left != null)
  49.                 {
  50.                     Clear(root.left);
  51.                     root.left = null;
  52.                 }
  53.                 if (root.right != null)
  54.                 {
  55.                     Clear(root.right);
  56.                     root.right = null;
  57.                 }                
  58.         }
  59.  
  60.         int l = 0, p = 0;
  61.         public void Wyswietl(Node root)
  62.         {            
  63.             Console.WriteLine("{0}\n", root.data);
  64.             if (root.left != null)
  65.             {
  66.                 ++l;
  67.                 Console.WriteLine("l: {0}", l);
  68.                 Wyswietl(root.left);
  69.                 p = 0;// ooooo
  70.             }
  71.             if (root.right != null)
  72.             {
  73.                 ++p;
  74.                 Console.WriteLine("p: {0}", p);
  75.                 Wyswietl(root.right);
  76.             }
  77.         }
  78.  
  79.         public bool Contains (int value, Node root)
  80.         {
  81.             if (value == root.data) return true;
  82.             if (value > root.data)
  83.             {
  84.                 if (root.right != null) return Contains(value, root.right);
  85.                 else return false;                
  86.             }
  87.             if (value < root.data)
  88.             {
  89.                 if (root.left != null) return Contains(value, root.left);
  90.                 else return false;                
  91.             }            
  92.             else return false;
  93.         }
  94.        
  95.         public Node GetMaxValueNode(Node root)
  96.         {
  97.          //   Node Temp;
  98.  
  99.             if (root.right != null)
  100.             {
  101.                 return GetMaxValueNode(root.right);
  102.                 //  return GetMaxValueNode(root.right);
  103.                 //Temp = GetMaxValueNode(root.right);
  104.                 //return Temp;
  105.             }
  106.             else return root;
  107.         }
  108.  
  109.          public int Zwrocdane (Node root)
  110.          {
  111.              return root.data;
  112.          }
  113.  
  114.  
  115.     };
  116.      
  117.     public class Drzewo
  118.     {
  119.         private Node root;
  120.         private int licznik;
  121.         private Node max;
  122.  
  123.         public Drzewo()
  124.         {
  125.             root = null;
  126.             licznik = 0;
  127.         }
  128.  
  129.         public int ZwrocWartosc(Node root)
  130.         {
  131.             return root.Zwrocdane(root);
  132.         }
  133.  
  134.         public Drzewo(int data)
  135.         {
  136.             root = new Node(data);
  137.         }
  138.  
  139.         public bool Contains(int value)
  140.         {
  141.             if (root.Contains(value, root)) return true;            
  142.             else return false;
  143.         }
  144.  
  145.         public void Add(int data)
  146.         {
  147.             root.Add(data);
  148.         }
  149.  
  150.         public void Clear(Drzewo tree)
  151.         {
  152.             root.Clear(root);
  153.             root = null;
  154.             tree = null;
  155.         }
  156.  
  157.         public void Wyswietl()
  158.         {
  159.             root.Wyswietl(root);
  160.         }
  161.  
  162.         public Node GetMaxValueNode()
  163.         {
  164.        //     Node Temp;
  165.        //     return root.GetMaxValueNode(root);
  166.        //     Temp= root.GetMaxValueNode(root);
  167.        //     return Temp;
  168.             return root.GetMaxValueNode(root);
  169.         }
  170.  
  171.        
  172.        
  173.     };
  174.  
  175.  
  176.     class Program
  177.     {
  178.         static void Main(string[] args)
  179.         {            
  180.             Drzewo drzewo = new Drzewo(20);
  181.             drzewo.Add(10);
  182.             drzewo.Add(5);
  183.             drzewo.Add(15);
  184.  
  185.             drzewo.Add(27);
  186.             drzewo.Add(25);
  187.             drzewo.Add(24);
  188.             drzewo.Add(22);
  189.             drzewo.Add(26);
  190.             drzewo.Add(30);
  191.             /*
  192.             for (int i = 0; i < 10; i++)
  193.             {
  194.                 Console.WriteLine("Podaj wartosc do wpsiania do drzewka");
  195.                 string a = Console.ReadLine();
  196.                 if (drzewo.Contains(Int16.Parse(a)).Equals(true)) Console.WriteLine("Ta cygfra juz jest");
  197.                 else Console.WriteLine("Wpisano");
  198.             }
  199.             Console.WriteLine("\n");
  200.             drzewo.Wyswietl();
  201.            */
  202.  
  203.  
  204.             Console.WriteLine("\n");
  205. //takie to no..
  206.             Console.WriteLine("Najwieksza wartosc {0}\n", drzewo.ZwrocWartosc(drzewo.GetMaxValueNode()));
  207.  
  208.             drzewo.Clear(drzewo);
  209.         }
  210.     }
  211. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment