Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Node<T> {
- public data: T;
- public left: Node<T> | null;
- public right: Node<T> | null;
- constructor(data: T) {
- this.data = data;
- this.left = null;
- this.right = null;
- }
- }
- enum TraversalMethod {
- PreOrder,
- InOrder,
- PostOrder
- }
- export default class BST<T> {
- private root: Node<T> | null;
- constructor() {
- this.root = null;
- }
- public insert = (data: T): void => {
- const newNode: Node<T> = new Node<T>(data);
- this.root = BST.insertionHelper(this.root, newNode);
- }
- private static insertionHelper = <T>(root: Node<T> | null, newNode: Node<T>): Node<T> => {
- if (root) {
- if (newNode.data < root.data) root.left = BST.insertionHelper(root.left, newNode);
- else if (newNode.data > root.data) root.right = BST.insertionHelper(root.right, newNode);
- return root;
- }
- return newNode;
- }
- public contains = (data: T): boolean => BST.containmentHelper(this.root, data);
- private static containmentHelper = <T>(root: Node<T> | null, data: T): boolean => {
- if (root) {
- if (data < root.data) return BST.containmentHelper(root.left, data);
- else if (data > root.data) return BST.containmentHelper(root.right, data);
- else return true;
- }
- return false;
- }
- public delete = (data: T): void => {
- this.root = BST.deletionHelper(this.root, data);
- }
- private static deletionHelper = <T>(root: Node<T> | null, data: T): Node<T> | null => {
- if (root) {
- if (data < root.data) root.left = BST.deletionHelper(root.left, data);
- else if (data > root.data) root.right = BST.deletionHelper(root.right, data);
- else {
- if (root.left && root.right) {
- let minInRight: Node<T> = BST.getNodeWithMinVal(root.right);
- root.data = minInRight.data;
- root.right = BST.deletionHelper(root.right, minInRight.data);
- }
- else if (root.left) root = root.left;
- else if (root.right) root = root.right;
- else root = null;
- }
- }
- return root;
- }
- public depthFirstTraverse = (taskFunc: (data: T) => unknown, traversalMethod: TraversalMethod): void => BST.depthFirstTraverseHelper(this.root, taskFunc, traversalMethod);
- private static depthFirstTraverseHelper = <T>(root: Node<T> | null, taskFunc: (data: T) => unknown, traversalMethod: TraversalMethod): void => {
- if (root) {
- if (traversalMethod === TraversalMethod.PreOrder) taskFunc(root.data);
- BST.depthFirstTraverseHelper(root.left, taskFunc, traversalMethod);
- if (traversalMethod === TraversalMethod.InOrder) taskFunc(root.data);
- BST.depthFirstTraverseHelper(root.right, taskFunc, traversalMethod);
- if (traversalMethod === TraversalMethod.PostOrder) taskFunc(root.data);
- }
- }
- public breadthFirstTraverse = (taskFunc: (data: T) => unknown): void => {
- if (this.root) {
- const queue: Array<Node<T>> = [this.root];
- while (queue.length) {
- const node: Node<T> = queue.splice(0, 1)[0]; // avoided queue.shift() since it returns Node<T> | undefined
- taskFunc(node.data);
- if (node.left) queue.push(node.left);
- if (node.right) queue.push(node.right);
- }
- }
- }
- public getMinValue = (): T | undefined => this.root ? BST.getNodeWithMinVal(this.root).data : undefined;
- private static getNodeWithMinVal = <T>(root: Node<T>): Node<T> => root.left ? BST.getNodeWithMinVal(root.left) : root;
- public getMaxValue = (): T | undefined => this.root ? BST.getNodeWithMaxVal(this.root).data : undefined;
- private static getNodeWithMaxVal = <T>(root: Node<T>): Node<T> => root.right ? BST.getNodeWithMaxVal(root.right) : root;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement