Binary search tree exact match - illustrated walkthrough

21
Binary Search Tree - Exact match Illustrated walk through

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.

Transcript of Binary search tree exact match - illustrated walkthrough

Page 1: Binary search tree   exact match - illustrated walkthrough

Binary Search Tree- Exact matchIllustrated walk through

Page 2: Binary search tree   exact match - illustrated walkthrough

Node structure

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

Value

Left Right

Page 3: Binary search tree   exact match - illustrated walkthrough

Sample tree5

3 10

1 4 7 12

Page 4: Binary search tree   exact match - illustrated walkthrough

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

Page 5: Binary search tree   exact match - illustrated walkthrough

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

Page 6: Binary search tree   exact match - illustrated walkthrough

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

Page 7: Binary search tree   exact match - illustrated walkthrough

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

Page 8: Binary search tree   exact match - illustrated walkthrough

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

Page 9: Binary search tree   exact match - illustrated walkthrough

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

Page 10: Binary search tree   exact match - illustrated walkthrough

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

Page 11: Binary search tree   exact match - illustrated walkthrough

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

Page 12: Binary search tree   exact match - illustrated walkthrough

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

Page 13: Binary search tree   exact match - illustrated walkthrough

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

Page 14: Binary search tree   exact match - illustrated walkthrough

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

Page 15: Binary search tree   exact match - illustrated walkthrough

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

Page 16: Binary search tree   exact match - illustrated walkthrough

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

Page 17: Binary search tree   exact match - illustrated walkthrough

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!

Page 18: Binary search tree   exact match - illustrated walkthrough

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

Page 19: Binary search tree   exact match - illustrated walkthrough

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

Page 20: Binary search tree   exact match - illustrated walkthrough

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

Page 21: Binary search tree   exact match - illustrated walkthrough

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