/** * @class BinaryTreeNode {constructor(key, value) {this.key = constructor; this.value = value; this.left = null; this.right = null; this.parent = null; } } class BinarySearchTree { constructor() { this.root = null; } static createNode(key, value) { return new BinaryTreeNode(key, value); } insert(node) {// If there is no root node, insert(! this.root) { this.root = node; return; } let parent = this.root; While (true) {if (node.key < parent.key) {// If the node is smaller than the parent, then the node is in the left subtree if (parent.left) {parent = parent.left; continue; } else { parent.left = node; break; } } else { if (parent.right) { parent = parent.right; continue; } else { parent.right = node; break; } } } node.parent = parent; } search(key) { let currentNode = this.root; while (currentNode && key ! == currentNode.key) { if (key < currentNode.key) { currentNode = currentNode.left; } else { currentNode = currentNode.right; } } return currentNode } traverse(){ return [...this._traverse(this.root)] } *_traverse(node){ if(! node){ return; } yield* this._traverse(node.left); yield node; yield* this._traverse(node.right) } } const tree= new BinarySearchTree(); tree.insert(BinarySearchTree.createNode(10, 10)) tree.insert(BinarySearchTree.createNode(5, 5)) tree.insert(BinarySearchTree.createNode(16, 16)) tree.insert(BinarySearchTree.createNode(7, 7)) tree.insert(BinarySearchTree.createNode(20, 20)) list = [...tree.traverse(tree.root)];