Binary Search Tree
- Exact match
Illustrated walk through
Node structure
public class Node
{
public int Value;
public Node Left;
public Node Right;
}

Value

Left

Right
Sample tree
5

3

1

10

4

7

12
Find Exact Match
public Node FindExact(Node node, int target) {
if (node == null) {
return null;
}
if (node.Value == targe...
node

target

5
3

1

10

4

7

12

public Node FindExact( Node node, int target) {
if (node == null) {
return null;
}
if ...
node

target

5
3

node == null
is false

1

10

4

7

12

public Node FindExact(Node node, int target) {
if (node == null...
node

target

5
3

5 == 7
is false

1

10

4

7

12

public Node FindExact(Node node, int target) {
if (node == null) {
re...
node

target

5
3

5<7
is true

1

10

4

7

12

public Node FindExact(Node node, int target) {
if (node == null) {
return...
node

target

5
3

1

7

10

4

7

12

Call Stack for 5
public Node FindExact(Node node, int target) {
if (node == null) {...
target

5
node

3

1

7

10

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact( Node node, int target) {
...
target

5
node

3

node == null
is false

1

7

10

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact(Nod...
target

5
node

3

1

10 == 7
is false

7

10

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact(Node nod...
target

5
node

3

1

10 < 7
is false

7

10

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact(Node node...
target

5
node

3

else (10 > 7)
is true

1

7

10

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact(Nod...
target

5
3

1

node

7

10

4

7

12
Call Stack for 7
Call Stack for 10
Call Stack for 5

public Node FindExact( Node nod...
node == null
is false

target

5
3

1

node

7

10

4

7

12
Call Stack for 7
Call Stack for 10
Call Stack for 5

public N...
7 == 7
is true
3

1

node

7

target

5
10

4

7

12
Call Stack for 7
Call Stack for 10
Call Stack for 5

public Node Find...
target

5
3

7

10
Call Stack for 7

1

node

4

7

12
Call Stack for 10
Call Stack for 5

public Node FindExact(Node node...
target

5
node

3

1

7

10

4

7

12
Call Stack for 10

7
Call Stack for 5
public Node FindExact(Node node, int target) {...
target

5

node
3

1

7

10

4

7

12

Call Stack for 5
public Node FindExact(Node node, int target) {
if (node == null) {...
target

5
3

1

7

10

4

7

12

public Node FindExact(Node node, int target) {
if (node == null) {
return null;
}

The ca...
Upcoming SlideShare
Loading in …5
×

Binary search tree exact match - illustrated walkthrough

723 views

Published on

A step-by-step illustration of Binary Search Tree (Exact Match) to help you walk through a series of operations. Illustration is accompanied by actual code with bold line indicating the current operation.

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Binary search tree exact match - illustrated walkthrough

  1. 1. Binary Search Tree - Exact match Illustrated walk through
  2. 2. Node structure public class Node { public int Value; public Node Left; public Node Right; } Value Left Right
  3. 3. Sample tree 5 3 1 10 4 7 12
  4. 4. Find Exact Match public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } Base Case: return when beyond a leaf return a node found Recursively search left or right subtree
  5. 5. node target 5 3 1 10 4 7 12 public Node FindExact( Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  6. 6. node target 5 3 node == null is false 1 10 4 7 12 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  7. 7. node target 5 3 5 == 7 is false 1 10 4 7 12 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  8. 8. node target 5 3 5<7 is true 1 10 4 7 12 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  9. 9. node target 5 3 1 7 10 4 7 12 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  10. 10. target 5 node 3 1 7 10 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact( Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  11. 11. target 5 node 3 node == null is false 1 7 10 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  12. 12. target 5 node 3 1 10 == 7 is false 7 10 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  13. 13. target 5 node 3 1 10 < 7 is false 7 10 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  14. 14. target 5 node 3 else (10 > 7) is true 1 7 10 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  15. 15. target 5 3 1 node 7 10 4 7 12 Call Stack for 7 Call Stack for 10 Call Stack for 5 public Node FindExact( Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  16. 16. node == null is false target 5 3 1 node 7 10 4 7 12 Call Stack for 7 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  17. 17. 7 == 7 is true 3 1 node 7 target 5 10 4 7 12 Call Stack for 7 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } We found an exact match! if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  18. 18. target 5 3 7 10 Call Stack for 7 1 node 4 7 12 Call Stack for 10 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  19. 19. target 5 node 3 1 7 10 4 7 12 Call Stack for 10 7 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } }
  20. 20. target 5 node 3 1 7 10 4 7 12 Call Stack for 5 public Node FindExact(Node node, int target) { if (node == null) { return null; } if (node.Value == target) { return node; } if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7
  21. 21. target 5 3 1 7 10 4 7 12 public Node FindExact(Node node, int target) { if (node == null) { return null; } The caller of the if (node.Value == FindExactreturn node; receives node 7} target) { if (node.Value < target) { return FindExact(node.Right, target); } else { return FindExact(node.Left, target); } } 7

×