SHARE
TWEET

Untitled

a guest Dec 3rd, 2019 146 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. type
  2.   BstNode = ref object
  3.     data: int
  4.     left: BstNode
  5.     right: BstNode
  6.  
  7. proc deleteNode(node: var BstNode, data: int): bool =
  8.   if node == nil:
  9.     return false
  10.   if data == node.data:
  11.     if node.left != nil and node.right != nil:  # Handle two children
  12.       #node.data = getMin(node.right, node.right).data
  13.       var tempnode = getMin(node.right, node.right)
  14.       var temp1 = node.left
  15.       var temp2: BstNode
  16.       var tempdata = tempnode.data
  17.       if tempnode.right != nil:
  18.         temp2 = node.right.right
  19.       else:
  20.         tempnode = nil
  21.         echo tempnode
  22.         echo $(node.right)
  23.         temp2 = node.right
  24.       echo node.right.data
  25.       node.data = tempdata
  26.       node.left = temp1
  27.       node.right = temp2
  28.       return true
  29.     if node.left != nil and node.right == nil:  # Handle left with single child
  30.       node.data = node.left.data
  31.       node.left = nil
  32.       return true
  33.     if node.left == nil and node.right != nil:  # Handle right with single child
  34.       node.data = node.right.data
  35.       node.right = nil
  36.       return true
  37.     if node.left == nil and node.right == nil:  # Handle leaf node
  38.       node = nil
  39.       return true
  40.   if data < node.data:
  41.     return deleteNode(node.left, data)
  42.   else:
  43.     return deleteNode(node.right, data)
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