March 2019
Intermediate to advanced
336 pages
9h 9m
English
The insertRNode method inserts the node and balances the tree. This method inserts rootNode with the KeyValue key, as presented in the following code snippet. The method takes rootNode, which is a TreeNode pointer, and the key as an integer as parameters. The method returns a TreeNode pointer and a Boolean value if the rootNode is inserted:
//insertRNode methodfunc insertRNode(rootNode *TreeNode, key KeyValue) (*TreeNode, bool) { if rootNode == nil { return &TreeNode{KeyValue: key}, false } var dir int dir = 0 if rootNode.KeyValue.LessThan(key) { dir = 1 } var done bool rootNode.LinkedNodes[dir], done = insertRNode(rootNode.LinkedNodes[dir], key) if done { return rootNode, true } rootNode.BalanceValue = rootNode.BalanceValue+(2*dir ...Read now
Unlock full access