github.com/djordje200179/extendedlibrary/datastructures@v1.7.1-0.20240227175559-d09520a92dd4/maps/rbt/node.go (about)

     1  package rbt
     2  
     3  type color bool
     4  
     5  const (
     6  	red   color = false
     7  	black color = true
     8  )
     9  
    10  // Node is a node of a red-black tree.
    11  // It should not be created directly.
    12  type Node[K, V any] struct {
    13  	key   K
    14  	Value V // Value is the value stored in the node.
    15  
    16  	color color
    17  
    18  	leftChild  *Node[K, V]
    19  	rightChild *Node[K, V]
    20  	parent     *Node[K, V]
    21  }
    22  
    23  // Key returns the key of the node.
    24  func (node *Node[K, V]) Key() K {
    25  	return node.key
    26  }
    27  
    28  // LeftChild returns the left child of the node.
    29  func (node *Node[K, V]) LeftChild() *Node[K, V] {
    30  	return node.leftChild
    31  }
    32  
    33  // RightChild returns the right child of the node.
    34  func (node *Node[K, V]) RightChild() *Node[K, V] {
    35  	return node.rightChild
    36  }
    37  
    38  // Parent returns the parent of the node.
    39  func (node *Node[K, V]) Parent() *Node[K, V] {
    40  	return node.parent
    41  }
    42  
    43  // Sibling returns the sibling of the node.
    44  // If the node is the root, it returns nil.
    45  func (node *Node[K, V]) Sibling() *Node[K, V] {
    46  	if node.parent == nil {
    47  		return nil
    48  	}
    49  
    50  	if node.parent.leftChild == node {
    51  		return node.parent.rightChild
    52  	} else {
    53  		return node.parent.leftChild
    54  	}
    55  }
    56  
    57  // Prev returns the previous node in the tree
    58  // in the order of the keys.
    59  func (node *Node[K, V]) Prev() *Node[K, V] {
    60  	if node.leftChild != nil {
    61  		return node.leftChild.Max()
    62  	}
    63  
    64  	for prev, curr := node.parent, node; prev != nil && curr == prev.leftChild; curr, prev = prev, prev.parent {
    65  		if curr != prev.leftChild {
    66  			return prev
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  // Next returns the next node in the tree
    74  // in the order of the keys.
    75  func (node *Node[K, V]) Next() *Node[K, V] {
    76  	if node.rightChild != nil {
    77  		return node.rightChild.Min()
    78  	}
    79  
    80  	for prev, curr := node.parent, node; prev != nil; curr, prev = prev, prev.parent {
    81  		if curr != prev.rightChild {
    82  			return prev
    83  		}
    84  	}
    85  
    86  	return nil
    87  }
    88  
    89  // Min returns the minimum node in the subtree rooted at the node.
    90  func (node *Node[K, V]) Min() *Node[K, V] {
    91  	for curr := node; curr != nil; curr = curr.leftChild {
    92  		if curr.leftChild == nil {
    93  			return curr
    94  		}
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  // Max returns the maximum node in the subtree rooted at the node.
   101  func (node *Node[K, V]) Max() *Node[K, V] {
   102  	for curr := node; curr != nil; curr = curr.rightChild {
   103  		if curr.rightChild == nil {
   104  			return curr
   105  		}
   106  	}
   107  
   108  	return nil
   109  }
   110  
   111  // Clone returns a clone of the node.
   112  // The clone has the same key, value, and color as the node.
   113  // The clone does not have any links to other nodes.
   114  func (node *Node[K, V]) Clone() *Node[K, V] {
   115  	return &Node[K, V]{
   116  		key:   node.key,
   117  		Value: node.Value,
   118  		color: node.color,
   119  	}
   120  }
   121  
   122  func nodeColor[K, V any](node *Node[K, V]) color {
   123  	if node == nil {
   124  		return black
   125  	}
   126  
   127  	return node.color
   128  }