Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Zad2_Y1_
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] t = { 4, 2, 3, 7, 1, 5, 6 };
- DrzewoBST<int> d0 = CreateTreeFromArray(t);
- Console.ReadLine();
- d0.ShowInOrder();
- Console.WriteLine("Skoro inorder to posortowana tablica, ");
- List<int> list = new List<int>();
- foreach (var VARIABLE in t)
- {
- list.Add(VARIABLE);
- }
- list.Sort();
- int a = 3;
- int b = 6; // tu liczymy ile elementow znajduje sie w przedziale [a,b] Dokończyć samemu
- Console.ReadKey();
- }
- public static DrzewoBST<T> CreateTreeFromArray<T>(T[] array) where T : IComparable<T>
- {
- DrzewoBST<T> tree = new DrzewoBST<T>();
- foreach (T item in array)
- {
- tree.Insert(item);
- }
- return tree;
- }
- }
- class DrzewoBST<T> where T : IComparable<T>
- {
- class Node
- {
- public T value;
- public Node left;
- public Node right;
- public Node(T value)
- {
- this.value = value;
- }
- }
- Node root;
- public DrzewoBST()
- {
- this.root = null;
- }
- // in-order
- public void ShowInOrder()
- {
- this.ShowInOrder(this.root);
- }
- private void ShowInOrder(Node node)
- {
- if (node == null) return;
- ShowInOrder(node.left);
- Console.Write(node.value + " ");
- ShowInOrder(node.right);
- }
- // Wstawianie rekurencyjne
- public void Insert(T value)
- {
- Node node = new Node(value);
- if (this.root == null)
- {
- this.root = node;
- }
- else
- {
- Insert(this.root, node);
- }
- }
- private void Insert(Node root, Node node)
- {
- if (root.value.CompareTo(node.value) > 0)
- {
- if (root.left == null)
- {
- root.left = node;
- }
- else
- {
- Insert(root.left, node);
- }
- }
- else
- {
- if (root.right == null)
- {
- root.right = node;
- }
- else
- {
- Insert(root.right, node);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement