English 中文(简体)
能够比特定关键因素更或更平等、能够减少关键因素的有效数据结构
原标题:Efficient data structure that returns smallest element greater or equal than given key and allows decrease key

是否有高效(log n)数据结构,可以开展以下工作:

  • Return the smallest element that is greater or equal to a given key
  • Exchange this element with a smaller one and rearrange the structure accordingly

元素的数量是众所周知的,在寿命期间不会改变。

最佳回答
问题回答

回答后,你可以使用双双双向树。 该法典可能有所帮助。

  1. 首先,你们必须找到树中的 no子。

     private Node<Key, Value> find (Node<Key, Value> node, Key key){
         if (node == null) return null;
    
         int comp = comparator.compare(key, node.key);
    
         if (comp < 0) return find(node.left, key);
         else if(comp > 0) return find(node.right, key);
         else return node;
     }
    
  2. 既然我们有 no子,我们就应当找到最小的钥匙,但比起关键点更重要或更平等,这意味着,如果 no子有权利的话,那将是一个更大的关键,但如果右 no不成,我们就必须归还钥匙。

     private Node<Key, Value> min(Node<Key, Value> node){
         if (node.left == null) return node;
         else return min(node.left);
     }
    
     public Key ceiling(Key key){ 
         Node<Key, Value> node = find(root, key);
         if (node.right == null) return node.key;
         return min(node.right).key;
     }
    

您询问的第二个项目是在你去除儿童身上的 no子时发生的,因此,它涉及他权利子女的小孩,并插入他的位置。

    private Node<Key, Value> remove(Node<Key, Value> node, Key key){
        if (node == null){
            throw new NoSuchElementException();
        }

        int comp = comparator.compare(key, node.key);

        if (comp > 0) node.right = remove(node.right, key);
        else if (comp < 0) node.left = remove(node.left, key);
        else{ //Found the node to remove

            if (node.right == null) return node.left; //Does not have right child. (if even does not have left child return null)
            else if (node.left == null) return node.right; //Does not have left child.
            else{ //Has right and left children

                Node<Key, Value> lower = min(node.right); //The lower node of its right child node
                node.key = lower.key;
                node.value = lower.value;
                remove(node.right, lower.key); //Remove the lower node that remains in the tree
            }
        }
        return node;
    }




相关问题
The Fastest DataStructure to Filter with in C#

Currently we are filtering and sorting data with a datatable. /// <summary> /// Filters the data table and returns a new data table with only the filtered rows. /// </summary>...

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Java large datastructure for storing a matrix

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then stored in an Excel file. ...

Holding onto items after a postback

I have an ASP.NET web application and I want to be able to take items from a master list and store them temporarliy into one of four other lists. The other lists need to survive post backs so that ...

negative number in the stack

I am a new student in the compilers world ^_^ and I want to know is legal represent negative number in the stack. For example: infix: 1-5=-4 postfix: 15- The statements are: push(1) push(5) x=...

What type of struct/container would you use in this instance?

I am trying to figure out what type of structure or container I should use for a quick project. I need to have an unknown number of sets that will be entered from the GUI (each one will have a name, ...

热门标签