Advertisement
Danielos168

Zad2(Wysokość drzewa)

Jan 23rd, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Zad2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] t = {4, 2, 3, 7, 1, 5, 6};
  14.             DrzewoBST<int> d0 = CreateTreeFromArray(t);
  15.            
  16.             Console.WriteLine(d0.Height());
  17.             Console.ReadKey();
  18.         }
  19.         public static DrzewoBST<T> CreateTreeFromArray<T>(T[] array) where T : IComparable<T>
  20.         {
  21.             DrzewoBST<T> tree = new DrzewoBST<T>();
  22.             foreach (T item in array)
  23.             {
  24.                 tree.Insert(item);
  25.             }
  26.             return tree;
  27.         }
  28.     }
  29.  
  30.     class DrzewoBST<T> where T : IComparable<T>
  31.     {
  32.         class Node
  33.         {
  34.             public T value;
  35.             public Node left;
  36.             public Node right;
  37.  
  38.             public Node(T value)
  39.             {
  40.                 this.value = value;
  41.             }
  42.         }
  43.  
  44.         Node root;
  45.  
  46.         public DrzewoBST()
  47.         {
  48.             this.root = null;
  49.         }
  50.  
  51.         // Wstawianie rekurencyjne
  52.         public void Insert(T value)
  53.         {
  54.             Node node = new Node(value);
  55.             if (this.root == null)
  56.             {
  57.                 this.root = node;
  58.             }
  59.             else
  60.             {
  61.                 Insert(this.root, node);
  62.             }
  63.         }
  64.         private void Insert(Node root, Node node)
  65.         {
  66.             if (root.value.CompareTo(node.value) > 0)
  67.             {
  68.                 if (root.left == null)
  69.                 {
  70.                     root.left = node;
  71.                 }
  72.                 else
  73.                 {
  74.                     Insert(root.left, node);
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 if (root.right == null)
  80.                 {
  81.                     root.right = node;
  82.                 }
  83.                 else
  84.                 {
  85.                     Insert(root.right, node);
  86.                 }
  87.             }
  88.         }
  89.  
  90.         // Wysokość drzewa
  91.         public int Height()
  92.         {
  93.             return Height(this.root);
  94.         }
  95.         private int Height(Node node)
  96.         {
  97.             int height = 0;
  98.             if (node == null) return 0;
  99.             if (node.left != null)
  100.             {
  101.                 height = Math.Max(height, Height(node.left) + 1);
  102.             }
  103.             if (node.right != null)
  104.             {
  105.                 height = Math.Max(height, Height(node.right) + 1);
  106.             }
  107.             return height;
  108.         }
  109.        
  110.     }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement