BST Insert

by Anton Bagayev

JavaScript

// equal values - to the right
function BSTInsert(tree, node) {
	let root = tree.root;
	BSTInsertRecur(node, nodeToInsert);
}

function BSTInsertRecur(node, nodeToInsert) {
	if(node.element < nodeToInsert.element) {
		if (node.leftChild == null) {
			node.leftChild = nodeToInsert;
		} else {
			BSTInsertRecur(node.leftChild, nodeToInsert);
		}
	} else {
		if (node.rightChild == null) {
			node.rightChild = nodeToInsert;
		} else {
			BSTInsertRecur(node.rightChild, nodeToInsert);
		}
	}
}