Example Binary Tree

Visual Aid


picture alt


Example Code


class TreeNode { constructor(val) { this.val = val; this.left = null; this.right = null; } } let a = new TreeNode("a"); let b = new TreeNode("b"); let c = new TreeNode("c"); let d = new TreeNode("d"); let e = new TreeNode("e"); let f = new TreeNode("f"); a.left = b; a.right = c; b.left = d; b.right = e; c.right = f;


Terms


Search Patterns


Constraints


PseudoCode For Insertion


PseudoCode For Search Of A single Item


PseudoCode For Breadth First Search Traversal


PseudoCode For Depth First Search Traversal

Pre-Order

Iterative

Recursive

In-Order

Iterative

Recursive

Post-Order

Iterative

Recursive


Example Binary Search Tree

class TreeNode { constructor(val) { this.val = val; this.left = null; this.right = null; } } class BST { constructor() { this.root = null; } //Insert a new node recursiveInsert(val, currentNode = this.root) { if (!this.root) { this.root = new TreeNode(val); return this; } if (val < currentNode.val) { if (!currentNode.left) { currentNode.left = new TreeNode(val); } else { this.insert(val, currentNode.left); } } else { if (!currentNode.right) { currentNode.right = new TreeNode(val); } else { this.insert(val, currentNode.right); } } } iterativeInsert(val, currentNode = this.root) { if (!this.root) { this.root = new TreeNode(val); return this; } if (val < currentNode.val) { if (!currentNode.left) { currentNode.left = new TreeNode(); } else { while (true) { if (val < currentNode.val) { if (!currenNodet.left) { currentNode.left = new TreeNode(); return this; } else { currentNode = currentNode.left; } } else { if (!currentNode.right) { currentNode.right = new TreeNode(); return this; } else { currentNode = currentNode.right; } } } } } } //Search the tree searchRecur(val, currentNode = this.root) { if (!currentNode) return false; if (val < currentNode.val) { return this.searchRecur(val, currentNode.left); } else if (val > currentNode.val) { return this.searchRecur(val, currentNode.right); } else { return true; } } searchIter(val) { let currentNode = this.root; while (currentNode) { if (val < currentNode.val) { currentNode = currentNode.left; } else if (val > currentNode.val) { currentNode = currentNode.right; } else { return true; } } return false; } // Maybe works, who knows, pulled it off the internet.... deleteNodeHelper(root, key) { if (root === null) { return false; } if (key < root.val) { root.left = deleteNodeHelper(root.left, key); return root; } else if (key > root.val) { root.right = deleteNodeHelper(root.right, key); return root; } else { if (root.left === null && root.right === null) { root = null; return root; } if (root.left === null) return root.right; if (root.right === null) return root.left; let currNode = root.right; while (currNode.left !== null) { currNode = currNode.left; } root.val = currNode.val; root.right = deleteNodeHelper(root.right, currNode.val); return root; } } //Recursive Depth First Search preOrderTraversal(root) { if (!root) return []; let left = this.preOrderTraversal(root.left); let right = this.preOrderTraversal(root.right); return [root.val, ...left, ...right]; } preOrderTraversalV2(root) { if (!root) return []; let newArray = new Array(); newArray.push(root.val); newArray.push(...this.preOrderTraversalV2(root.left)); newArray.push(...this.preOrderTraversalV2(root.right)); return newArray; } inOrderTraversal(root) { if (!root) return []; let left = this.inOrderTraversal(root.left); let right = this.inOrderTraversal(root.right); return [...left, root.val, ...right]; } inOrderTraversalV2(root) { if (!root) return []; let newArray = new Array(); newArray.push(...this.inOrderTraversalV2(root.left)); newArray.push(root.val); newArray.push(...this.inOrderTraversalV2(root.right)); return newArray; } postOrderTraversal(root) { if (!root) return []; let left = this.postOrderTraversal(root.left); let right = this.postOrderTraversal(root.right); return [...left, ...right, root.val]; } postOrderTraversalV2(root) { if (!root) return []; let newArray = new Array(); newArray.push(...this.postOrderTraversalV2(root.left)); newArray.push(...this.postOrderTraversalV2(root.right)); newArray.push(root.val); return newArray; } // Iterative Depth First Search iterativePreOrder(root) { let stack = [root]; let results = []; while (stack.length) { let current = stack.pop(); results.push(current); if (current.left) stack.push(current.left); if (current.right) stack.push(current.right); } return results; } iterativeInOrder(root) { let stack = []; let current = root; let results = []; while (true) { while (current) { stack.push(current); current = current.left; } if (!stack.length) break; let removed = stack.pop(); results.push(removed); current = current.right; } return results; } //To-Do iterativePostOrder //Breadth First Search breadthFirstSearch(root) { let queue = [root]; let result = []; while (queue.length) { let current = queue.shift(); if (current.left) queue.push(current.left); if (current.right) queue.push(current.left); current.push(result); } return result; } // Converting a Sorted Array to a Binary Search Tree sortedArrayToBST(nums) { if (nums.length === 0) return null; let mid = Math.floor(nums.length / 2); let root = new TreeNode(nums[mid]); let left = nums.slice(0, mid); root.left = sortedArrayToBST(left); let right = nums.slice(mid + 1); root.right = sortedArrayToBST(right); return root; } }