March 2019
Intermediate to advanced
336 pages
9h 9m
English
The removeBalance method removes the balance factor in a tree. This method adjusts the balance factor after removing the node and returns a treeNode pointer and a Boolean if the balance is removed. The method takes rootNode (an instance of TreeNode) and nodeValue (an integer) as parameters. This is shown in the following code:
// removeBalance methodfunc removeBalance(rootNode *TreeNode, nodeValue int) (*TreeNode, bool) { var node *TreeNode node = rootNode.LinkedNodes[opposite(nodeValue)] var balance int balance = 2*nodeValue - 1 switch node.BalanceValue { case -balance: rootNode.BalanceValue = 0 node.BalanceValue = 0 return singleRotation(rootNode, nodeValue), false case balance: adjustBalance(rootNode, opposite(nodeValue), ...Read now
Unlock full access