Advertisement
Danielos168

Zad2(Y2)

Jan 23rd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 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_Y1_
  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.             Console.ReadLine();
  16.             d0.ShowInOrder();
  17.             Console.WriteLine("Skoro inorder to posortowana tablica, ");
  18.             List<int> list = new List<int>();
  19.             foreach (var VARIABLE in t)
  20.             {
  21.                 list.Add(VARIABLE);
  22.             }
  23.             list.Sort();
  24.             int a = 3;
  25.             int b = 6; // tu liczymy ile elementow znajduje sie w przedziale [a,b] Dokończyć samemu
  26.  
  27.             Console.ReadKey();
  28.         }
  29.         public static DrzewoBST<T> CreateTreeFromArray<T>(T[] array) where T : IComparable<T>
  30.         {
  31.             DrzewoBST<T> tree = new DrzewoBST<T>();
  32.             foreach (T item in array)
  33.             {
  34.                 tree.Insert(item);
  35.             }
  36.             return tree;
  37.         }
  38.     }
  39.  
  40.     class DrzewoBST<T> where T : IComparable<T>
  41.     {
  42.         class Node
  43.         {
  44.             public T value;
  45.             public Node left;
  46.             public Node right;
  47.  
  48.             public Node(T value)
  49.             {
  50.                 this.value = value;
  51.             }
  52.         }
  53.  
  54.         Node root;
  55.  
  56.         public DrzewoBST()
  57.         {
  58.             this.root = null;
  59.         }
  60.  
  61.         // in-order
  62.         public void ShowInOrder()
  63.         {
  64.             this.ShowInOrder(this.root);
  65.         }
  66.         private void ShowInOrder(Node node)
  67.         {
  68.             if (node == null) return;
  69.             ShowInOrder(node.left);
  70.             Console.Write(node.value + " ");
  71.             ShowInOrder(node.right);
  72.         }
  73.  
  74.  
  75.         // Wstawianie rekurencyjne
  76.         public void Insert(T value)
  77.         {
  78.             Node node = new Node(value);
  79.             if (this.root == null)
  80.             {
  81.                 this.root = node;
  82.             }
  83.             else
  84.             {
  85.                 Insert(this.root, node);
  86.             }
  87.         }
  88.         private void Insert(Node root, Node node)
  89.         {
  90.             if (root.value.CompareTo(node.value) > 0)
  91.             {
  92.                 if (root.left == null)
  93.                 {
  94.                     root.left = node;
  95.                 }
  96.                 else
  97.                 {
  98.                     Insert(root.left, node);
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 if (root.right == null)
  104.                 {
  105.                     root.right = node;
  106.                 }
  107.                 else
  108.                 {
  109.                     Insert(root.right, node);
  110.                 }
  111.             }
  112.         }
  113.     }
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement