github.com/Nigel2392/go-datastructures@v1.1.5/binarytree/interfaced_bst_node.go (about)

     1  package binarytree
     2  
     3  import "github.com/Nigel2392/go-datastructures"
     4  
     5  type IF_BSTNode[T datastructures.Comparable[T]] struct {
     6  	value T
     7  	left  *IF_BSTNode[T]
     8  	right *IF_BSTNode[T]
     9  }
    10  
    11  func (n *IF_BSTNode[T]) Value() T {
    12  	return n.value
    13  }
    14  
    15  func (n *IF_BSTNode[T]) insert(v T) (inserted bool) {
    16  	// Equals
    17  	if n.value.Lt(v) {
    18  		if n.right == nil {
    19  			n.right = &IF_BSTNode[T]{value: v}
    20  			return true
    21  		} else {
    22  			return n.right.insert(v)
    23  		}
    24  	} else if v.Lt(n.value) { // Gt(
    25  		if n.left == nil {
    26  			n.left = &IF_BSTNode[T]{value: v}
    27  			return true
    28  		} else {
    29  			return n.left.insert(v)
    30  		}
    31  	} else if !n.value.Lt(v) && !v.Lt(n.value) {
    32  		n.value = v
    33  	}
    34  	return false
    35  }
    36  
    37  func (n *IF_BSTNode[T]) search(value T) (v T, ok bool) {
    38  	// if we've reached the end of the tree, the value is not present
    39  	if n == nil {
    40  		return
    41  	}
    42  	// check if we need to traverse further down the tree
    43  	if n.value.Lt(value) {
    44  		return n.right.search(value)
    45  	} else if value.Lt(n.value) {
    46  		return n.left.search(value)
    47  	} else if !n.value.Lt(value) && !value.Lt(n.value) {
    48  		// value is not less than or greater than, so it must be equal
    49  		return n.value, true
    50  	}
    51  	return
    52  }
    53  
    54  func (n *IF_BSTNode[T]) traverse(f func(T)) {
    55  	if n == nil {
    56  		return
    57  	}
    58  
    59  	n.left.traverse(f)
    60  	f(n.value)
    61  	n.right.traverse(f)
    62  }
    63  
    64  func (n *IF_BSTNode[T]) delete(v T) (newRoot *IF_BSTNode[T], deleted bool) {
    65  	if n == nil {
    66  		return nil, false
    67  	}
    68  
    69  	if n.value.Lt(v) {
    70  		n.right, deleted = n.right.delete(v)
    71  	} else if v.Lt(n.value) {
    72  		n.left, deleted = n.left.delete(v)
    73  	} else if !n.value.Lt(v) && !v.Lt(n.value) {
    74  		deleted = true
    75  		if n.left == nil {
    76  			return n.right, deleted
    77  		} else if n.right == nil {
    78  			return n.left, deleted
    79  		}
    80  
    81  		minRight := n.right.findMin()
    82  		minRight.right, _ = n.right.delete(minRight.value)
    83  		minRight.left = n.left
    84  		return minRight, deleted
    85  	}
    86  	return n, deleted
    87  }
    88  
    89  func (n *IF_BSTNode[T]) deleteIf(predicate func(T) bool) (newRoot *IF_BSTNode[T], deleted int) {
    90  	if n == nil {
    91  		return nil, 0
    92  	}
    93  
    94  	n.left, deleted = n.left.deleteIf(predicate)
    95  	n.right, deleted = n.right.deleteIf(predicate)
    96  
    97  	if predicate(n.value) {
    98  		deleted++
    99  		if n.left == nil {
   100  			return n.right, deleted
   101  		} else if n.right == nil {
   102  			return n.left, deleted
   103  		}
   104  
   105  		minRight := n.right.findMin()
   106  		minRight.right, _ = n.right.delete(minRight.value)
   107  		minRight.left = n.left
   108  		return minRight, deleted
   109  	}
   110  
   111  	return n, deleted
   112  }
   113  
   114  func (n *IF_BSTNode[T]) findMin() *IF_BSTNode[T] {
   115  	current := n
   116  	for current.left != nil {
   117  		current = current.left
   118  	}
   119  	return current
   120  }
   121  
   122  func (n *IF_BSTNode[T]) getHeight() int {
   123  	if n == nil {
   124  		return 0
   125  	}
   126  
   127  	leftHeight := n.left.getHeight()
   128  	rightHeight := n.right.getHeight()
   129  
   130  	if leftHeight > rightHeight {
   131  		return leftHeight + 1
   132  	}
   133  
   134  	return rightHeight + 1
   135  }