Как реализовать очередь с максимальным приоритетом, используя двоичное дерево с упорядоченной кучей и трижды связанным узлом? - PullRequest
1 голос
/ 17 марта 2020

Я пытаюсь реализовать очередь с максимальным приоритетом, используя двоичное дерево кучи с тройным узлом. Это код, который у меня сейчас есть, когда я запускаю его и пытаюсь распечатать дерево, ничего не печатать, это просто пустые строки. Я использую методы помогали тонуть и плавать, чтобы помочь мне организовать очередь при добавлении различных элементов. Я также реализую ADT (MaxPQ), который просто имеет методы publi c, которые должны быть реализованы. Мне было интересно, если я что-то делаю не так?

public class LinkedMaxPQ<T extends Comparable<T>> implements MaxPQ<T> {

  // Instance variables
  Node root;
  int size;
  Node lastInserted;

  // Node inner class definition

  // Node class
  class Node {
    int N;
    T info;
    Node left;
    Node right;
    Node parent;

    Node(T info, int N) {
      this.info = info; this.N = N;
    }
  }

  private void swim(Node x){
    if(x == null) return;
    if(x.parent == null) return; // we're at root
    int cmp = x.info.compareTo(x.parent.info);
    if(cmp > 0){
      swapNodeData(x, x.parent);
      swim(x.parent);
    }
  }
  private void swapNodeData(Node x, Node y){
    T temp = x.info;
    x.info = y.info;
    y.info = temp;
  }
  private void sink(Node x){
    if(x == null) return;
    Node swapNode;
    if(x.left == null && x.right == null){
      return;
    }
    else if(x.left == null){
      swapNode = x.right;
      int cmp = x.info.compareTo(swapNode.info);
      if(cmp < 0)
      swapNodeData(swapNode, x);
    } else if(x.right == null){
      swapNode = x.left;
      int cmp = x.info.compareTo(swapNode.info);
      if(cmp < 0)
      swapNodeData(swapNode, x);
    } else{
      int cmp = x.left.info.compareTo(x.right.info);
      if(cmp >= 0){
        swapNode = x.left;
      } else{
        swapNode = x.right;
      }
      int cmpParChild = x.info.compareTo(swapNode.info);
      if(cmpParChild < 0) {
        swapNodeData(swapNode, x);
        sink(swapNode);
      }
    }
  }
  String printThisLevel (Node rootnode, int level) {
    StringBuilder s = new StringBuilder();

    // Base case 1: if the current rootnode is null, return the current string.
    if (rootnode == null) {
      return s.toString();
    }

    // Base case 2: If you're at the first level, append the
    // info field of the current rootnode.
    if (level == 1) {
      s.append( rootnode.info.toString());
    }
    // Recursive calls: otherwise call the method on the left
    // and on the right of the next lower level.
    else if (level > 1)  {
      s.append( printThisLevel(rootnode.left, level-1));
      s.append( printThisLevel(rootnode.right, level-1));
    }
    return s.toString();
  }

  private int size(Node x){
    if(x == null) return 0;
    return x.N;
  }

  private Node insert(Node x, T data){
    if(x == null){
      lastInserted = new Node(data, 1);
      return lastInserted;
    }
    // compare left and right sizes see where to go
    int leftSize = size(x.left);
    int rightSize = size(x.right);

    if(leftSize <= rightSize){
      // go to left
      Node inserted = insert(x.left, data);
      x.left = inserted;
      inserted.parent = x;
    } else{
      // go to right
      Node inserted = insert(x.right, data);
      x.right = inserted;
      inserted.parent = x;
    }
    x.N = size(x.left) + size(x.right) + 1;
    return x;
  }
  private Node resetLastInserted(Node x){
    if(x == null) return null;
    if(x.left == null && x.right == null) return x;
    if(size(x.right) < size(x.left))return resetLastInserted(x.left);
    else                            return resetLastInserted(x.right);
  }

  public void insert(T data){
    root = insert(root, data);
    swim(lastInserted);
  }
  public T getMax(){
    if(root == null) return null;
    return root.info;
  }
  public T removeMax(){
    if(size() == 1){
      T ret = root.info;
      root = null;
      return ret;
    }
    swapNodeData(root, lastInserted);
    Node lastInsParent = lastInserted.parent;
    T lastInsData = lastInserted.info;
    if(lastInserted == lastInsParent.left){
      lastInsParent.left = null;
    } else{
      lastInsParent.right = null;
    }

    Node traverser = lastInserted;

    while(traverser != null){
      traverser.N--;
      traverser = traverser.parent;
    }

    lastInserted = resetLastInserted(root);

    sink(root);

    return lastInsData;
  }
  public int size(){
    return size(root);
  }
  public boolean isEmpty(){
    return size() == 0;
  }


  public String toString() {
    // Create a StringBuilder object to make it more efficient.
    StringBuilder sb=new StringBuilder();

    // get the height of the tree
    int height = (int)Math.ceil(Math.log(size+1) / Math.log(2));

    // for each level in the tree, call printThisLevel and
    // append the output to the StringBuilder
    for (int i=1; i<=height; i++) {
      sb.append("level " + i + ": "+ printThisLevel(this.root, i) + "\n");
    }

    // Return the string of the StringBuilder object
    return sb.toString();
  }


  public static void main (String[] args) {
    LinkedMaxPQ<String> t = new LinkedMaxPQ<String>();
    t.insert("a");
    System.out.println(t.toString());
    t.insert("b");
    t.insert("c");
    t.insert("d");
    t.insert("e");
    t.insert("f");
    t.insert("g");
    t.insert("h");
    t.insert("i");
    t.insert("j");
    t.insert("k");
    t.size();
    t.removeMax();
    t.getMax();
    t.removeMax();
    t.insert("x");
    t.insert("y");
    t.removeMax();
    t.getMax();
    System.out.println(t.toString());

  }

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...