SHARE
TWEET

Untitled

a guest Sep 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private fun remove(node: TreeNode<K, V>?, key: K?): TreeNode<K, V>? {
  2.         val keyHashCode = key.hashCode()
  3.         val nodeHasCode = node?.key?.hashCode() ?: 0
  4.  
  5.         if (keyHashCode < nodeHasCode) {
  6.             node?.left = remove(node?.left, key)
  7.         } else if (keyHashCode > nodeHasCode) {
  8.             node?.right = remove(node?.right, key)
  9.         } else {
  10.             if (node?.left == null && node?.right == null) {
  11.                 return null
  12.             } else if (node.left == null) {
  13.                 return node.right
  14.             } else if (node.right == null) {
  15.                 return node.left
  16.             } else {
  17.                 val minNode = minValue(node.right)
  18.                 node.key = minNode?.key
  19.                 node.value = minNode?.value
  20.                 node.right = remove(node.right, minNode?.key)
  21.             }
  22.         }
  23.  
  24.         return node
  25.     }
  26.  
  27.     private fun minValue(node: TreeNode<K, V>?): TreeNode<K, V>? {
  28.         if (node?.left != null) {
  29.             return minValue(node.left)
  30.         }
  31.  
  32.         return node
  33.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top