java - Red-black tree - How to find the node's parent? -


In the red-black tree, when rotated, you will need to know who is the root of the specific node, however, only in the node The reference to the right or left child is

I was thinking of giving a node frequency variable "parents", but for this reason I do not think that it is justified to do this and it is the root cause for rotation The context will be very complex to change.

  Public square node {Private left; Personal rights; Private IRD; Private parents; // I do not think that's a good idea)  

So, searching for my solution to apologize is to find the parent () method to use. I'm wondering what is the other way to find the node's parents?

My solution:

Sample tree:

  50/25 75  

If you have node 25 If you want to find the parents of, you pass something like this:

  node guardian = finder (Node25.value);  

And this node returns 550.

  Find the protected node parent (int val) {if (root == null) {return null; } Node current = root; Node original = current; While (true) {if found (current.val == val) {// found return parent; } If (current == faucet) {// not found reverse tap; } Original = current; If (current.val> wal) {// right current = present Left; } Else {// right turn on = current right; }}}}  

I was thinking of giving a node instance variable "Parents" But for this reason I do not think it is worth doing.

An additional pointer / reference for the original context near your nodes Per Node is required

  • When you want to know the parents for a given node, cross the paste Compare it with the requirement. Additional references, and as long as you modify the node, keep it up to date.
  • Computational cost and complexity to cross the tree to find the parents of the given node
  • P> I think the choice between these two options is something subjective But personally I only choose to keep track of parents references.

    In your reference, java.util.TreeMap is implemented as a red-black tree which is the entry node in which left , right , and original context.


    Comments

    Popular posts from this blog

    c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

    Calling GetGUIThreadInfo from Outlook VBA -

    Obfuscating Python code? -