Advertisement
habur331

Untitled

Apr 3rd, 2022
1,020
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.58 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javafx.util.Pair;
  4.  
  5. import java.util.ArrayList;
  6. import java.util.Comparator;
  7. import java.util.List;
  8.  
  9. public class Main
  10. {
  11.  
  12.     public static void main(String[] args)
  13.     {
  14.         BTree<Integer, Integer> tree = new BTree<>(4);
  15.         tree.add(1, 1);
  16.         tree.add(2, 2);
  17.         tree.add(3, 3);
  18.         tree.add(4, 0);
  19.         tree.add(5, 0);
  20.         tree.add(6, 0);
  21.         tree.add(7, 0);
  22.         tree.add(8, 0);
  23.         tree.add(9, 0);
  24.         tree.add(10, 0);
  25.         tree.add(11, 0);
  26.         tree.add(12, 0);
  27.         tree.add(13, 0);
  28.  
  29.         System.out.println(tree.lookupRange(1, 2));
  30.     }
  31. }
  32.  
  33. interface RangeMap<K, V>
  34. {
  35.     int size();
  36.  
  37.     boolean isEmpty();
  38.  
  39.     void add(K key, V value); // insert new item into the map
  40.  
  41.     boolean contains(K key); // check if a key is present
  42.  
  43.     V lookup(K key); // lookup a value by the key
  44.  
  45.     List<V> lookupRange(K from, K to); // lookup values for a range of keys
  46.  
  47.     // optional: remove an item from a map (+1% extra credit)
  48.     Object remove(K key);
  49. }
  50.  
  51. class BTree<K extends Comparable<K>, V extends Number> implements RangeMap<K, V>
  52. {
  53.     private BNode<K, V> root = null;
  54.  
  55.     public final int B;
  56.     private int size = 0;
  57.  
  58.     static class BNode<K extends Comparable<K>, V extends Number>
  59.     {
  60.         private final BTree<K, V> tree;
  61.         private final int B;
  62.         private BNode<K, V> parent;
  63.         private final List<Pair<K, V>> nodes;
  64.         private final List<BNode<K, V>> children;
  65.  
  66.         public BNode(BNode<K, V> parent, BTree<K, V> tree)
  67.         {
  68.             this.tree = tree;
  69.             this.B = tree.B;
  70.             this.parent = parent;
  71.             this.nodes = new ArrayList<>(B);
  72.             this.children = new ArrayList<>(B + 1);
  73.         }
  74.  
  75.         public void addKey(K newKey, V newValue)
  76.         {
  77.             if (children.size() == 0)
  78.             {
  79.                 add(newKey, newValue);
  80.             }
  81.             else
  82.             {
  83.                 //Most left node
  84.                 if (nodes.get(0).getKey().compareTo(newKey) >= 0)
  85.                 {
  86.                     children.get(0).addKey(newKey, newValue);
  87.                 }
  88.  
  89.                 //Most right node
  90.                 if (nodes.get(nodes.size() - 1).getKey().compareTo(newKey) < 0)
  91.                 {
  92.                     children.get(children.size() - 1).addKey(newKey, newValue);
  93.                 }
  94.  
  95.                 //nodes.size() <= B-1
  96.                 for (int i = 1; i < nodes.size(); i++)
  97.                 {
  98.                     if (nodes.get(i - 1).getKey().compareTo(newKey) >= 0 && nodes.get(i).getKey().compareTo(newKey) < 0)
  99.                     {
  100.                         children.get(i).addKey(newKey, newValue);
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private void add(K newKey, V newValue)
  107.         {
  108.             this.nodes.add(new Pair<>(newKey, newValue));
  109.             nodes.sort(Comparator.comparing(Pair::getKey));
  110.  
  111.             if (this.nodes.size() == B)
  112.                 split();
  113.         }
  114.  
  115.         private void split()
  116.         {
  117.             if (nodes.size() == B)
  118.             {
  119.                 int index = B % 2 == 0 ? B / 2 - 1 : B / 2;
  120.                 Pair<K, V> medianNode = nodes.get(index);
  121.  
  122.                 BNode<K, V> leftNode = new BNode<>(parent, tree);
  123.                 /// удаляем ноды слева от пивота и добавляем их группой к детям родителя
  124.                 for (int i = 0; i < index; i++)
  125.                 {
  126.                     //перезаписываем ноды
  127.                     leftNode.nodes.add(nodes.get(0));
  128.                     nodes.remove(0);
  129.                 }
  130.  
  131.                 // перезаписываем детей в leftNode
  132.                 if (children.size() != 0)
  133.                 {
  134.                     for (int i = 0; i <= index; i++)
  135.                     {
  136.                         //перезаписываем детей
  137.                         leftNode.children.add(children.get(0));
  138.                         children.remove(0);
  139.                     }
  140.                 }
  141.                 ///Нужно проверить куда он смещает элемент, который был на этом индекс
  142.                 if (parent == null)
  143.                 {
  144.                     BNode<K, V> newParent = new BNode<>(null, tree);
  145.                     parent = newParent;
  146.                     leftNode.parent = newParent;
  147.                     parent.children.add(this);
  148.                     tree.root = newParent;
  149.                 }
  150.                 parent.children.add(parent.children.indexOf(this), leftNode);
  151.                 parent.nodes.add(medianNode);
  152.                 // pivot стал нулевым
  153.                 nodes.remove(0);
  154.                 parent.nodes.sort(Comparator.comparing(Pair::getKey));
  155.  
  156.                 parent.split();
  157.             }
  158.         }
  159.  
  160.         public V lookup(K key)
  161.         {
  162.             int index = -1;
  163.             for (int i = 0; i < nodes.size(); i++)
  164.             {
  165.                 if (nodes.get(i).getKey() == key)
  166.                     index = i;
  167.             }
  168.  
  169.             V returnValue = null;
  170.  
  171.             if (index == -1)
  172.             {
  173.                 if (children.size() > 0)
  174.                 {
  175.                     V value;
  176.                     if (nodes.get(0).getKey().compareTo(key) >= 0)
  177.                     {
  178.                         value = children.get(0).lookup(key);
  179.                         if (value != null)
  180.                             returnValue = value;
  181.                     }
  182.  
  183.                     //Most right node
  184.                     if (nodes.get(nodes.size() - 1).getKey().compareTo(key) < 0)
  185.                     {
  186.                         value = children.get(children.size() - 1).lookup(key);
  187.                         if (value != null)
  188.                             returnValue = value;
  189.                     }
  190.  
  191.                     //nodes.size() <= B-1
  192.                     for (int i = 1; i < nodes.size(); i++)
  193.                     {
  194.                         if (nodes.get(i - 1).getKey().compareTo(key) >= 0 && nodes.get(i).getKey().compareTo(key) < 0)
  195.                         {
  196.                             value = children.get(i).lookup(key);
  197.                             if (value != null)
  198.                                 returnValue = value;
  199.                         }
  200.                     }
  201.                 }
  202.             }
  203.             else
  204.                 returnValue = nodes.get(index).getValue();
  205.  
  206.  
  207.             return returnValue;
  208.         }
  209.  
  210.         public List<V> lookupRange(K from, K to)
  211.         {
  212.             List<V> returnList = new ArrayList<>();
  213.  
  214.             for (int j = 0; j < nodes.size(); j++)
  215.             {
  216.                 //Most left node
  217.                 if (nodes.get(0).getKey().compareTo(from) >= 0)
  218.                 {
  219.                     returnList.add(nodes.get(0).getValue());
  220.  
  221.                     if (children.size() > 0)
  222.                     {
  223.                         List<V> t = children.get(0).lookupRange(from, to);
  224.                         if (t != null)
  225.                             returnList.addAll(t);
  226.                     }
  227.                 }
  228.  
  229.                 //Most right node
  230.                 if (nodes.get(nodes.size() - 1).getKey().compareTo(from) < 0)
  231.                 {
  232.                     return null;
  233.                 }
  234.  
  235.                 //nodes.size() <= B-1
  236.                 for (int i = 1; i < nodes.size(); i++)
  237.                 {
  238.                     if (nodes.get(i - 1).getKey().compareTo(from) >= 0 && nodes.get(i).getKey().compareTo(from) < 0)
  239.                     {
  240.                         if (nodes.get(i - 1).getKey().compareTo(from) >= 0)
  241.                             returnList.add(nodes.get(i - 1).getValue());
  242.  
  243.                         if (nodes.get(i).getKey().compareTo(from) >= 0)
  244.                             returnList.add(nodes.get(i - 1).getValue());
  245.  
  246.                         if (children.size() > 0)
  247.                         {
  248.                             List<V> t = children.get(i).lookupRange(from, to);
  249.                             if (t != null)
  250.                                 returnList.addAll(t);
  251.                         }
  252.                     }
  253.                 }
  254.  
  255.                 if (nodes.get(0).getKey().compareTo(to) >= 0)
  256.                 {
  257.                     return null;
  258.                 }
  259.  
  260.                 //Most right node
  261.                 if (nodes.get(nodes.size() - 1).getKey().compareTo(to) < 0)
  262.                 {
  263.                     returnList.add(nodes.get(nodes.size() - 1).getValue());
  264.                     if (children.size() > 0)
  265.                     {
  266.                         List<V> t = children.get(0).lookupRange(from, to);
  267.                         if (t != null)
  268.                             returnList.addAll(t);
  269.                     }
  270.                 }
  271.  
  272.                 //nodes.size() <= B-1
  273.                 for (int i = 1; i < nodes.size(); i++)
  274.                 {
  275.                     if (nodes.get(i - 1).getKey().compareTo(to) >= 0 && nodes.get(i).getKey().compareTo(to) < 0)
  276.                     {
  277.                         if (nodes.get(i - 1).getKey().compareTo(to) >= 0)
  278.                             returnList.add(nodes.get(i - 1).getValue());
  279.  
  280.                         if (nodes.get(i).getKey().compareTo(to) >= 0)
  281.                             returnList.add(nodes.get(i - 1).getValue());
  282.                         if (children.size() > 0)
  283.                         {
  284.                             List<V> t = children.get(i).lookupRange(from, to);
  285.                             if (t != null)
  286.                                 returnList.addAll(t);
  287.                         }
  288.                     }
  289.                 }
  290.             }
  291.             return null;
  292.         }
  293.     }
  294.  
  295.     public BTree(int B)
  296.     {
  297.         this.B = B;
  298.     }
  299.  
  300.     @Override
  301.     public int size()
  302.     {
  303.         return size;
  304.     }
  305.  
  306.     @Override
  307.     public boolean isEmpty()
  308.     {
  309.         return size() == 0;
  310.     }
  311.  
  312.     @Override
  313.     public void add(K key, V value)
  314.     {
  315.         size++;
  316.  
  317.         if (root == null)
  318.             root = new BNode<>(null, this);
  319.  
  320.         root.addKey(key, value);
  321.  
  322.     }
  323.  
  324.     @Override
  325.     public boolean contains(K key)
  326.     {
  327.         return lookup(key) != null;
  328.     }
  329.  
  330.     @Override
  331.     public V lookup(K key)
  332.     {
  333.         return root.lookup(key);
  334.     }
  335.  
  336.     @Override
  337.     public List<V> lookupRange(K from, K to)
  338.     {
  339.         return root.lookupRange(from, to);
  340.     }
  341.  
  342.     @Override
  343.     public Object remove(K key)
  344.     {
  345.         return null;
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement