Modified 2-3-4 tree - algorithms

466 Views Asked by At

I haven 2-3-4 tree but it's modified that only leaves has values. I am not sure if leaves is exact words. Leaves (in my description) are nodes at bottom of tree with maximum depth (these are nodes at the end of tree). Each leave has one value. Other nodes (not leaves) has "value" of smallest value of his children nodes. So this is valid tree:

      2
    /   \
  3      2
/ | \  / | \
8 3 4  7 2  5

I need to write a few pseudocodes which has maximal complexity O(log n). I have to write minimum (returns minimum from tree), insert(k) (insert new leave with k value), decrease key (x,k) (change key in list x to k), delete (x) remove leave from tree and extract min (remove leave with minimal value).

I've tried to write something on my own but I am not sure if I am doing this right and if I satisfy condition with complexity.

class Node234 {
int value;
int count;
Node234 []childrens;
Node234 parent;
}

Node234 minimum()
{
  minimum(root);
}

Node234 minimum(Node234 node)
{
  if(node.count == 0)
  {
    return node;
  }

  for(i = 0; i < node.count; i++)
  {
     if(node.value == node.childrens[i].value)
        return minimum(node.childrens[i]);
  }
}

insert(int newValue)
{
  insert(newValue, root);
}

insert(int newValue, Node234 node)
{
  if(node.count == 0)
  {
    Node234 newNode = new Node234(newValue);
    insert(newNode, newValue, node);
  }
  else
  {
     insert(newValue, node.childrens[node.count-1]);
  }
}

insert(Node234 newNode, int newValue, Node234 node)
{
if(node.parent == null)
{
  Node234 newNodeParent = new Node234(newValue);
  newNodeParent.childrens[0] = node;
  node.parent = newNodeParent;
  newNodeParent.childrens[1] = newNode;
  newNode.parent = newNodeParent;
  if(node.value < newNode.value)
    newNodeParent.value = node.value;
}
else
{
  if(node.parent.count == 4) 
  {
     Node234 newNodeParent = new Node234(newValue);
     node.parent.count--;
      if(node.parent.value == node.value)
      {
         setMinForNode(node.parent);
      }
      newNodeParent.childrens[0] = node;
      node.parent = newNodeParent;
      newNodeParent.childrens[1] = newNode;
      newNode.parent = newNodeParent;

      if(node.value < newValue)
        newNodeParent.value = node.value;
      insert(newNodeParent, newNode.value, node.parent);
  }
  else
  {
    node.parent.childrens[node.parent.count] = newNode;
      node.parent.count++;
      if(newNode.value < node.parent.value)
      {
        changeAllParentKeys(newNode, newNode.value);
      }
  }
}
}

changeAllParentKeys(Node234 node, int newValue)
{
  if(node.parent != null && node.parent.value > newValue)
  {
    node.parent.value = newValue;
    changeAllParentKeys(node.parent, newValue);
  }
}

setMinForNode(Node234 parent)
{
  parent.value = parent.childrens[0].value;
  for(i = 1; i < parent.count; i++)
  {
     if(parent.value > parent.childrens[i].value)
        parent.value = parent.childrens[i].value;
  }
}

I hope minimum function is okay but is it insert function okay? Isn't too complex? Could you help me with satisfy complexity condition? Can you help me with other functions (algorithm in words would be enough :) ).

0

There are 0 best solutions below