Binary Tree Examples
Our visualization tool brings binary tree operations to life through an intuitive graphical interface. Each node is represented as a circle with the value displayed inside, connected by lines representing the parent-child relationships. This hierarchical visualization makes it easy to understand tree structures and operations at a glance.
A key feature of our tool is its dynamic node tracking. As your code traverses the tree, whether during insertion, deletion, or search operations, the tool highlights the current node and its connections. This visual feedback makes it particularly useful for learning tree manipulation concepts or debugging tree-related algorithms.
Let's look at some common binary tree algorithms and their visualizations:
Tree Traversal
function inorderTraversal(root) {
const result = [];
// @ignore-function-tree
function traverse(node) {
if (node === null) return;
traverse(node.left);
result.push(node.val);
traverse(node.right);
}
traverse(root);
return result;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
inorderTraversal(root); // [2, 1, 3]
function preorderTraversal(root) {
const result = [];
// @ignore-function-tree
function traverse(node) {
if (node === null) return;
result.push(node.val);
traverse(node.left);
traverse(node.right);
}
traverse(root);
return result;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
preorderTraversal(root); // [1, 2, 3]
function postorderTraversal(root) {
const result = [];
// @ignore-function-tree
function traverse(node) {
if (node === null) return;
traverse(node.left);
traverse(node.right);
result.push(node.val);
}
traverse(root);
return result;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
postorderTraversal(root); // [2, 3, 1]
function levelOrderTraversal(root) {
if (!root) return [];
const result = [];
const queue = new Queue();
queue.enqueue(root);
while (queue.size > 0) {
const levelSize = queue.size;
const currentLevel = [];
for (let i = 0; i < levelSize; i++) {
const node = queue.dequeue();
currentLevel.push(node.val);
if (node.left) queue.enqueue(node.left);
if (node.right) queue.enqueue(node.right);
}
result.push(currentLevel);
}
return result;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
levelOrderTraversal(root); // [[1], [2, 3]]
Generating interactive preview...
Binary Search Tree Operations
// @ignore-function-tree
function insertIntoBST(root, val) {
if (root === null) {
return new TreeNode(val);
}
if (val < root.val) {
root.left = insertIntoBST(root.left, val);
} else {
root.right = insertIntoBST(root.right, val);
}
return root;
}
// Example usage:
let root = new TreeNode(5);
insertIntoBST(root, 3);
insertIntoBST(root, 7);
// @ignore-function-tree
function searchBST(root, val) {
if (root === null || root.val === val) {
return root;
}
if (val < root.val) {
return searchBST(root.left, val);
}
return searchBST(root.right, val);
}
// Example usage:
const root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(7);
searchBST(root, 3); // Returns node with value 3
// @ignore-function-tree
function deleteFromBST(root, val) {
if (root === null) return null;
if (val < root.val) {
root.left = deleteFromBST(root.left, val);
} else if (val > root.val) {
root.right = deleteFromBST(root.right, val);
} else {
// Node with only one child or no child
if (root.left === null) return root.right;
if (root.right === null) return root.left;
// Node with two children
let minNode = findMin(root.right);
root.val = minNode.val;
root.right = deleteFromBST(root.right, minNode.val);
}
return root;
}
function findMin(node) {
while (node.left !== null) {
node = node.left;
}
return node;
}
// Example usage:
let root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(7);
deleteFromBST(root, 3);
Generating interactive preview...
Tree Properties
// @ignore-function-tree
function getHeight(root) {
if (root === null) return 0;
const leftHeight = getHeight(root.left);
const rightHeight = getHeight(root.right);
return Math.max(leftHeight, rightHeight) + 1;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
getHeight(root); // 3
function isBalanced(root) {
// @ignore-function-tree
function checkHeight(node) {
if (node === null) return 0;
const leftHeight = checkHeight(node.left);
if (leftHeight === -1) return -1;
const rightHeight = checkHeight(node.right);
if (rightHeight === -1) return -1;
if (Math.abs(leftHeight - rightHeight) > 1) return -1;
return Math.max(leftHeight, rightHeight) + 1;
}
return checkHeight(root) !== -1;
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
isBalanced(root); // true
function isSymmetric(root) {
if (root === null) return true;
// @ignore-function-tree
function isMirror(left, right) {
if (left === null && right === null) return true;
if (left === null || right === null) return false;
return (left.val === right.val) &&
isMirror(left.left, right.right) &&
isMirror(left.right, right.left);
}
return isMirror(root.left, root.right);
}
// Example usage:
const root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(2);
isSymmetric(root); // true
Generating interactive preview...