Binary search tree exact match - illustrated walkthrough
-
Upload
yoshi-watanabe -
Category
Technology
-
view
374 -
download
0
description
Transcript of Binary search tree exact match - illustrated walkthrough
![Page 1: Binary search tree exact match - illustrated walkthrough](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/1.jpg)
Binary Search Tree- Exact matchIllustrated walk through
![Page 2: Binary search tree exact match - illustrated walkthrough](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/3.jpg)
Sample tree5
3 10
1 4 7 12
![Page 4: Binary search tree exact match - illustrated walkthrough](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/4.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/6.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/15.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.us/reader033/viewer/2022051209/549a3e58b47959794d8b5904/html5/thumbnails/21.jpg)
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