March 2019
Intermediate to advanced
336 pages
9h 9m
English
The adjustBalance method adjusts the balance of the tree. In the following code snippet, the adjustBalance method does a double rotation given the balance factor, rootNode, and nodeValue. The adjustBalance method takes rootNode, which is an instance of the TreeNode type, nodeValue, and balanceValue (which are both integers) as parameters:
// adjust balance method func adjustBalance(rootNode *TreeNode, nodeValue int, balanceValue int) { var node *TreeNode node = rootNode.LinkedNodes[nodeValue] var oppNode *TreeNode oppNode = node.LinkedNodes[opposite(balanceValue)] switch oppNode.BalanceValue { case 0: rootNode.BalanceValue = 0 node.BalanceValue = 0 case balanceValue: rootNode.BalanceValue = -balanceValue node.BalanceValue ...Read now
Unlock full access