Guest User

Untitled

a guest
Apr 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace BST_Array
  7. {
  8.     class BST
  9.     {
  10.         private const int MAX_TREE_SIZE = 6;
  11.         private const int NULL_INDEX = -999;
  12.  
  13.         private int[] keys, left, right, parent;
  14.         private bool[] visited;
  15.         private int treeSize;
  16.  
  17.         public BST()
  18.         {
  19.             keys = new int[MAX_TREE_SIZE];
  20.             left = new int[MAX_TREE_SIZE];
  21.             right = new int[MAX_TREE_SIZE];
  22.             parent = new int[MAX_TREE_SIZE];
  23.  
  24.             visited = new bool[MAX_TREE_SIZE];
  25.             treeSize = 0;
  26.         }
  27.  
  28.         public void Add(int key)
  29.         {
  30.  
  31.             this.keys[this.treeSize] = key;
  32.             this.left[this.treeSize] = NULL_INDEX;
  33.             this.right[this.treeSize] = NULL_INDEX;
  34.  
  35.             if (this.treeSize == 0)
  36.             {
  37.                 this.parent[this.treeSize] = NULL_INDEX;
  38.             }
  39.             else
  40.             {
  41.                 this.insert(treeSize, 0);
  42.             }
  43.  
  44.             this.treeSize++;
  45.         }
  46.  
  47.         public int Max()
  48.         {
  49.             if (this.treeSize == 0)
  50.             { // if tree has no elements
  51.                 return 0;
  52.             }
  53.             else
  54.             {
  55.                 return this.maxValue(0);
  56.             }
  57.         }
  58.  
  59.         private int maxValue(int index)
  60.         {
  61.             if (this.right[index] == NULL_INDEX)
  62.             {
  63.                 return this.keys[index];
  64.             }
  65.             else
  66.             {
  67.                 return this.maxValue(right[index]);
  68.             }
  69.         }
  70.  
  71.         public int Min()
  72.         {
  73.             if (this.treeSize == 0)
  74.             { // if tree has no elements
  75.                 return 0;
  76.             }
  77.             else
  78.             {
  79.                 return this.minValue(0);
  80.             }
  81.         }
  82.  
  83.         private int minValue(int index)
  84.         {
  85.             if (this.left[index] == NULL_INDEX)
  86.             {
  87.                 return this.keys[index];
  88.             }
  89.             else
  90.             {
  91.                 return this.minValue(left[index]);
  92.             }
  93.         }
  94.  
  95.         private void insert(int keyIndex, int parentIndex)
  96.         {
  97.             if (this.keys[keyIndex] <= this.keys[parentIndex])
  98.             {// go left
  99.                 if (this.left[parentIndex] != NULL_INDEX)
  100.                 {
  101.                     this.insert(keyIndex, this.left[parentIndex]);
  102.                 }
  103.                 else
  104.                 {
  105.                     this.left[parentIndex] = keyIndex;
  106.                     this.parent[keyIndex] = parentIndex;
  107.                 }
  108.             }
  109.             else
  110.             {// go right
  111.                 if (this.right[parentIndex] != NULL_INDEX)
  112.                 {
  113.                     this.insert(keyIndex, this.right[parentIndex]);
  114.                 }
  115.                 else
  116.                 {
  117.                     this.right[parentIndex] = keyIndex;
  118.                     this.parent[keyIndex] = parentIndex;
  119.                 }
  120.             }
  121.  
  122.  
  123.         }
  124.     }
  125. }
Add Comment
Please, Sign In to add comment