0

I am creating a heap from a binary tree in java. My implementation looks like this:

public class Heap <P extends Comparable<P>,D> {
  // ATTRIBUTES
  private TreeNode<P,D> root;
  private int size;

  // CONSTRUCTOR
  PriorityQueue() {
    this.root = null;
    this.last = null;
    this.size = 0;
  }

  class TreeNode<P,D> {
    // ATTRIBUTES
    P priority;
    D data;
    TreeNode<P,D> left;
    TreeNode<P,D> right;

    // CONSTRUCTOR
    TreeNode(P priority, D data, TreeNode<P,D> left, TreeNode<P,D> right) {
      this.priority = priority;
      this.data = data;
      this.left = left;
      this.right = right;
    }
    TreeNode(P priority, D data) {
      this(priority, data, null, null);
    }
}

Now I want to get the nth element of my node. I thought, that it would be smart to convert n to a binary string, pop the first 1 and then go left for each 0 and right for each 1.

That doesn't seem too hard, but now my problem is, how I should create an output like like root.left.left.right to get to the 9th element. I don't just wanna get a copy, that is easy (see the getNode(int n) function below), I want a reference to that node, so I can add a new node in that spot for example.

public TreeNode<P,D> getNode(int n) {
  String binString = Integer.toBinaryString(n);
  char[] binChars = binString.substring(1, binString.length()).toCharArray();
  TreeNode<P,D> node = this.root;
  for( char c : getNodePath(n) ){
    if( c == '0' ){
      node = node.left;
    } else if( c == '1' ){
      node = node.right;
    }
  }
  return node;
}

Is that even possible in java? In python I would just create a ".left.left.right" String and use run, but that doesn't seem to be possible in java.

MrLoh
  • 456
  • 1
  • 5
  • 12

1 Answers1

0

The way you've structured your data, what you need is not a reference to the node n (which you are already getting), but a reference to its parent.

So for .left.left.right

you need to cut off the last bit and call getNode with .left.left

that will give you your Node's parent Node. The target node itself you can refer to by using the returned parent node and using the bit you cut off (.right)

parent.right

Also that way you can reassign the node by parent.right = new-node

btw, you don't need to create a binary string to implement your scheme. You just need to use the shift operators and 'bitwise AND'

ac3
  • 196
  • 2
  • so, why do I need to go to the parent, what is the problem with just going to the child? And also how do I best return the parent together with a number for left or right, since there are no tuples in java. (sorry I'm new to java) – MrLoh Jun 29 '14 at 20:50
  • oh, gotcha. I need to change the right on the parent of course, to really change the right of the parent, dough. And I can just go to the parent with n//2 and get the left or right value with n%2. – MrLoh Jun 29 '14 at 21:02
  • yeah thats more or less what you've to do, but I get a feeling you're mixing stuff and doing more than necessary. For instance if you are going with the binary scheme, then getNode(n >> 1) will give you your parent and (n & 1) will tell you if it is left or right child you are after. Both are bitwise operators - should be same in python. I don't see you've accepted the answer but anyway good to know that was approx what you were looking for. And yeah, a new language will take some time getting used to. – ac3 Jun 29 '14 at 21:35
  • I will accept the answer, once I get everything running. So what I don't get yet is how to implement the getNode just with bitwise operations, since I need to start from the left of the number, but can just shift away the right parts with >> – MrLoh Jun 29 '14 at 21:45
  • Ok the original stuff was not about bitwise. However since I mentioned using the bitwise operators let me just give you one way to do that before I leave your implementation :). `boolean isMSBSet(int i){ return (Integer.MIN_VALUE & i) < 0; }` So if you call in a loop isMSBSet( i << count) you should be able to inspect the characters from left to right – ac3 Jun 29 '14 at 22:19