Untitled
unknown
plain_text
9 months ago
857 B
4
Indexable
class Node { constructor(key) { this.key = key; this.left = null; this.right = null; } } const a = new Node(3); const b = new Node(2); const c = new Node(4); const d = new Node(0); const f = new Node(1); const e = new Node(5); a.left = b; a.right = c; b.left = d; b.right = f; c.right = e; // Tree : // 3 // / \ // 2 4 // / \ \ // 0 1 5 function search(root, key) { if (root === null || root.key === key) { return root; } if (root.key < key) { return search(root.right, key); } return search(root.left, key); } const result = search(a, 1); if (result) { console.log("Node found:", result.val); } else { console.log("Node not found"); }
Editor is loading...
Leave a Comment