March 2019
Intermediate to advanced
336 pages
9h 9m
English
The insertTreenode method inserts the new TreeNode in the binary search tree. In the following code, the insertTreeNode method takes rootNode and newTreeNode, both of the TreeNode type, as parameters. Note that newTreeNode is inserted at the right place in the binary search tree by comparing the key values:
// insertTreeNode functionfunc insertTreeNode(rootNode *TreeNode, newTreeNode *TreeNode) { if newTreeNode.key < rootNode.key { if rootNode.leftNode == nil { rootNode.leftNode = newTreeNode } else { insertTreeNode(rootNode.leftNode, newTreeNode) } } else { if rootNode.rightNode == nil{ rootNode.rightNode = newTreeNode } else { insertTreeNode(rootNode.rightNode, newTreeNode) } }}
Read now
Unlock full access