Weitere ähnliche Inhalte Ähnlich wie Binary search tree exact match - illustrated walkthrough (20) Mehr von Yoshi Watanabe (8) Kürzlich hochgeladen (20) Binary search tree exact match - illustrated walkthrough4. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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