March 2019
Intermediate to advanced
336 pages
9h 9m
English
MaxNode finds the node with maximum property in the binary search tree. The RLock method of the tree lock instance is called first and the RUnlock method on the tree lock instance is deferred. The MaxNode method returns the element with the highest value after traversing from rootNode and finding a rightNode with a nil value. This is shown in the following code:
// MaxNode methodfunc (tree *BinarySearchTree) MaxNode() *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.rightNode == nil { return &treeNode.value } treeNode = treeNode.rightNode }}
Read now
Unlock full access