March 2019
Intermediate to advanced
336 pages
9h 9m
English
MinNode finds the node with the minimum value in the binary search tree. In the following code snippet, the RLock method of the tree lock instance is invoked first and the RUnlock method on the tree lock instance is deferred. The MinNode method returns the element with the lowest value by traversing from rootNode and checking whether the value of leftNode is nil:
// MinNode methodfunc (tree *BinarySearchTree) MinNode() *int { tree.lock.RLock() defer tree.lock.RUnlock() var treeNode *TreeNode treeNode = tree.rootNode if treeNode == nil { //nil instead of 0 return (*int)(nil) } for { if treeNode.leftNode == nil { return &treeNode.value } treeNode = treeNode.leftNode }}
Read now
Unlock full access