61
loading...
This website collects cookies to deliver better user experience
left subtree < parent <= right subtree
).search(data) {
// empty tree
if (!root) return;
// start from root node
let current = root;
while (current.data !== data) {
// data greater than current element
if (data > current.data) {
// you are on a leaf node, nowhere to go
if (!current.rightNode) return;
// go to the right subtree
current = current.rightNode;
} else {
// you are on a leaf node, nowhere to go
if (!current.leftNode) return;
// go to the left subtree
current = current.leftNode;
}
}
return current;
}
insert(data) {
// empty tree, insert the first node (the root node)
if (!root) {
root = new Node(data);
return root;
}
// start from the root node
let current = root;
while (true) {
if (data > current.data) {
// search for an empty position in the right subtree
if (current.rightNode) {
current = current.rightNode;
} else {
// insert node
current.rightNode = new Node(data);
return current.rightNode;
}
} else {
// search for an empty position in the left subtree
if (current.leftNode) {
current = current.leftNode;
} else {
// insert node
current.leftNode = new Node(data);
return current.leftNode;
}
}
}
}
inOrderTraversal(node) {
if (node) {
inOrderTraversal(node.leftNode);
console.log(node.data);
inOrderTraversal(node.rightNode);
}
}
preOrderTraversal(node) {
if (node) {
console.log(node.data);
this.preOrderTraversal(node.leftNode);
this.preOrderTraversal(node.rightNode);
}
}
postOrderTraversal(node) {
if (node) {
this.postOrderTraversal(node.leftNode);
this.postOrderTraversal(node.rightNode);
console.log(node.data);
}
}
export class BinarySearchTreeNode<T> {
data: T;
leftNode?: BinarySearchTreeNode<T>;
rightNode?: BinarySearchTreeNode<T>;
constructor(data: T) {
this.data = data;
}
}
export class BinarySearchTree<T> {
root?: BinarySearchTreeNode<T>;
comparator: (a: T, b: T) => number;
constructor(comparator: (a: T, b: T) => number) {
this.comparator = comparator;
}
insert(data: T): BinarySearchTreeNode<T> | undefined {
if (!this.root) {
this.root = new BinarySearchTreeNode(data);
return this.root;
}
let current = this.root;
while (true) {
if (this.comparator(data, current.data) === 1) {
if (current.rightNode) {
current = current.rightNode;
} else {
current.rightNode = new BinarySearchTreeNode(data);
return current.rightNode;
}
} else {
if (current.leftNode) {
current = current.leftNode;
} else {
current.leftNode = new BinarySearchTreeNode(data);
return current.leftNode;
}
}
}
}
search(data: T): BinarySearchTreeNode<T> | undefined {
if (!this.root) return undefined;
let current = this.root;
while (this.comparator(data, current.data) !== 0) {
if (this.comparator(data, current.data) === 1) {
if (!current.rightNode) return;
current = current.rightNode;
} else {
if (!current.leftNode) return;
current = current.leftNode;
}
}
return current;
}
inOrderTraversal(node: BinarySearchTreeNode<T> | undefined): void {
if (node) {
this.inOrderTraversal(node.leftNode);
console.log(node.data);
this.inOrderTraversal(node.rightNode);
}
}
preOrderTraversal(node: BinarySearchTreeNode<T> | undefined): void {
if (node) {
console.log(node.data);
this.preOrderTraversal(node.leftNode);
this.preOrderTraversal(node.rightNode);
}
}
postOrderTraversal(node: BinarySearchTreeNode<T> | undefined): void {
if (node) {
this.postOrderTraversal(node.leftNode);
this.postOrderTraversal(node.rightNode);
console.log(node.data);
}
}
}
function comparator(a: number, b: number) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
const bst = new BinarySearchTree(comparator);
bst.insert(5);
bst.insert(2);
bst.insert(3);
bst.insert(1);
bst.insert(7);
bst.insert(6);
bst.insert(8);
bst.inOrderTraversal(bst.root);