Binary search tree exact match - illustrated walkthrough

Preview:

DESCRIPTION

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.

Citation preview

Binary Search Tree- Exact matchIllustrated walk through

Node structure

public class Node{ public int Value; public Node Left; public Node Right;}

Value

Left Right

Sample tree5

3 10

1 4 7 12

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 leafreturn a node found

Recursively search left or right subtree

5

3 10

1 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); }}

7targetnode

5

3 10

1 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); }}

7targetnode

node == nullis false

5

3 10

1 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); }}

7targetnode

5 == 7is false

5

3 10

1 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); }}

7targetnode

5 < 7is true

5

3 10

1 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); }}

7targetnode

Call Stack for 5

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

node == nullis false

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

10 == 7is false

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

10 < 7is false

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

else (10 > 7)is true

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

Call Stack for 7

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

Call Stack for 7

node == nullis false

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

Call Stack for 7

7 == 7is true

We found an exact match!

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 10

Call Stack for 77

5

3 10

1 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); }}

7target

node

Call Stack for 5

Call Stack for 107

5

3 10

1 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); }}

7targetnode

Call Stack for 57

5

3 10

1 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); }}

7target

7

The caller of the FindExact receives node 7