Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.46 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. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace Phone
  17. {
  18.     /// <summary>
  19.     /// Interaktionslogik für MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         private DataCreator dataCreator = new DataCreator();
  24.         public Dictionary<long, string> data = new Dictionary<long, string>();
  25.         long[] keys = { 0L };
  26.         private Tree MyTree;
  27.         public int nodeCounter = 0;
  28.  
  29.         public MainWindow()
  30.         {
  31.             InitializeComponent();
  32.  
  33.             dataCreator.create();
  34.  
  35.             keys = dataCreator.data.Keys.ToArray();
  36.             Array.Sort(keys);
  37.  
  38.             foreach(long key in keys)
  39.             {
  40.                 data.Add(key, dataCreator.data[key]);
  41.             }
  42.  
  43.             MyTree = createTree(data, keys);
  44.             int i = nodeCounter;
  45.             int j = 0;
  46.         }
  47.  
  48.         private void _return_Click(object sender, RoutedEventArgs e)
  49.         {
  50.             long key = Convert.ToInt64(this._input.Text);
  51.             string result = MyTree.search(key, MyTree.Root);
  52.             this._result.Text = result;
  53.         }
  54.  
  55.         private Tree createTree(Dictionary<long, string> items, long[] keys)
  56.         {
  57.             long key = keys[(int)(Math.Ceiling((double)((double)keys.Length / (double)2)))];
  58.             List<long> keysForUse = keys.ToList();
  59.             keysForUse.Remove(key);
  60.             Node root = new Node(key, items[key]);
  61.             Tree myTree = new Tree(root);
  62.  
  63.             int leftLength = (int)(Math.Floor((double)((double)keysForUse.Count / (double)2)));
  64.             int rightLength = (int)(Math.Ceiling((double)((double)keysForUse.Count / (double)2)));
  65.  
  66.             long[] forLeft = new long[leftLength];
  67.             long[] forRight = new long[rightLength];
  68.             int counter = 0;
  69.             int i = 0;
  70.  
  71.             for (i = 0; counter < (keysForUse.Count / 2); i++)
  72.             {
  73.                 forLeft[i] = keys[counter++];
  74.             }
  75.  
  76.             for (i = 0; counter < keysForUse.Count; i++)
  77.             {
  78.                 forRight[i] = keys[counter++];
  79.             }
  80.  
  81.             createLeft(forLeft[(int)(Math.Floor((double)((double)forLeft.Length / (double)2)))], items, myTree.Root, forLeft, myTree);
  82.             createRight(forRight[(int)(Math.Floor((double)((double)forRight.Length / (double)2)))], items, myTree.Root, forRight, myTree);
  83.  
  84.             return myTree;
  85.         }
  86.  
  87.         private void createLeft(long key, Dictionary<long, string> items, Node parent, long[] keysLeft, Tree tree)
  88.         {
  89.             List<long> keys = keysLeft.ToList();
  90.             tree.add_node(key, items[key], tree.Root);
  91.             nodeCounter++;
  92.             keys.Remove(key);
  93.  
  94.             int leftLength = (int)(Math.Floor((double)((double)keys.Count / (double)2)));
  95.             int rightLength = (int)(Math.Ceiling((double)((double)keys.Count / (double)2)));
  96.  
  97.             long[] forLeft = new long[leftLength];
  98.             long[] forRight = new long[rightLength];
  99.             long[] left = keys.ToArray();
  100.             int counter = 0;
  101.             int i = 0;
  102.  
  103.             for (i = 0; counter < (left.Length / 2); i++)
  104.             {
  105.                 forLeft[i] = left[counter++];
  106.             }
  107.  
  108.             for (i = 0; counter < left.Length; i++)
  109.             {
  110.                 forRight[i] = left[counter++];
  111.             }
  112.  
  113.             if (leftLength != 0)
  114.             {
  115.                 createLeft(forLeft[(int)(Math.Floor((double)((double)forLeft.Length / (double)2)))], items, tree.Root, forLeft, tree);
  116.             }
  117.             if (rightLength != 0)
  118.             {
  119.                 createRight(forRight[(int)(Math.Floor((double)((double)forRight.Length / (double)2)))], items, tree.Root, forRight, tree);
  120.             }
  121.  
  122.         }
  123.  
  124.         private void createRight(long key, Dictionary<long, string> items, Node parent, long[] keysLeft, Tree tree)
  125.         {
  126.             List<long> keys = keysLeft.ToList();
  127.             tree.add_node(key, items[key], tree.Root);
  128.             nodeCounter++;
  129.             keys.Remove(key);
  130.  
  131.             int leftLength = (int)(Math.Floor((double)((double)keys.Count / (double)2)));
  132.             int rightLength = (int)(Math.Ceiling((double)((double)keys.Count / (double)2)));
  133.  
  134.             long[] forLeft = new long[leftLength];
  135.             long[] forRight = new long[rightLength];
  136.             long[] left = keys.ToArray();
  137.             int counter = 0;
  138.             int i = 0;
  139.  
  140.             for (i = 0; counter < (left.Length / 2); i++)
  141.             {
  142.                 forLeft[i] = left[counter++];
  143.             }
  144.  
  145.             for (i = 0; counter < left.Length; i++)
  146.             {
  147.                 forRight[i] = left[counter++];
  148.             }
  149.  
  150.             if (leftLength != 0)
  151.             {
  152.                 createLeft(forLeft[(int)(Math.Floor((double)((double)forLeft.Length / (double)2)))], items, tree.Root, forLeft, tree);
  153.             }
  154.             if (rightLength != 0)
  155.             {
  156.                 createRight(forRight[(int)(Math.Floor((double)((double)forRight.Length / (double)2)))], items, tree.Root, forRight, tree);
  157.             }
  158.  
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement