SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
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 == 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
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
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
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
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
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
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
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);
}
}
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
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

Mais conteúdo relacionado

Mais procurados

重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1
Chris Huang
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
Chris Huang
 

Mais procurados (20)

重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1重構—改善既有程式的設計(chapter 8)part 1
重構—改善既有程式的設計(chapter 8)part 1
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
Prog
ProgProg
Prog
 
c programming
c programmingc programming
c programming
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Qno 1 (d)
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
Tai lieu ky thuat lap trinh
Tai lieu ky thuat lap trinhTai lieu ky thuat lap trinh
Tai lieu ky thuat lap trinh
 
Mutation Testing at BzhJUG
Mutation Testing at BzhJUGMutation Testing at BzhJUG
Mutation Testing at BzhJUG
 
Trie Data Structure
Trie Data StructureTrie Data Structure
Trie Data Structure
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
c programming
c programmingc programming
c programming
 
FSOFT - Test Java Exam
FSOFT - Test Java ExamFSOFT - Test Java Exam
FSOFT - Test Java Exam
 

Semelhante a Binary search tree exact match - illustrated walkthrough

Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Footageetoffe16
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
feelingcomputors
 
Hello- the following code- In LeftRotaate and RightRotate methods for.pdf
Hello- the following code- In LeftRotaate and RightRotate methods for.pdfHello- the following code- In LeftRotaate and RightRotate methods for.pdf
Hello- the following code- In LeftRotaate and RightRotate methods for.pdf
a2zmobiles
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
archiesgallery
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
Stewart29UReesa
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
hadpadrrajeshh
 
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
package edu-ser222-m03_02-   ---  - A binary search tree based impleme.docxpackage edu-ser222-m03_02-   ---  - A binary search tree based impleme.docx
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
farrahkur54
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
mail931892
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
michardsonkhaicarr37
 
Java code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdfJava code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdf
fashioncollection2
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
annaipowerelectronic
 

Semelhante a Binary search tree exact match - illustrated walkthrough (20)

Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdfAssignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
Assignment 9 (Parent reference for BST) Redefine TreeNode by adding .pdf
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
 
Hello- the following code- In LeftRotaate and RightRotate methods for.pdf
Hello- the following code- In LeftRotaate and RightRotate methods for.pdfHello- the following code- In LeftRotaate and RightRotate methods for.pdf
Hello- the following code- In LeftRotaate and RightRotate methods for.pdf
 
Using the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docxUsing the following definition for a Binary Tree Node - complete the f.docx
Using the following definition for a Binary Tree Node - complete the f.docx
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
 
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdfimport java-util-Iterator- import java-util-NoSuchElementException- im.pdf
import java-util-Iterator- import java-util-NoSuchElementException- im.pdf
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
package edu-ser222-m03_02-   ---  - A binary search tree based impleme.docxpackage edu-ser222-m03_02-   ---  - A binary search tree based impleme.docx
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)Swift에서 꼬리재귀 사용기 (Tail Recursion)
Swift에서 꼬리재귀 사용기 (Tail Recursion)
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
Please change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdfPlease change this method to recursive method.  public String post.pdf
Please change this method to recursive method.  public String post.pdf
 
#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx#include iostream using namespace std; const int nil = 0; cl.docx
#include iostream using namespace std; const int nil = 0; cl.docx
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Binary tree in java
Binary tree in javaBinary tree in java
Binary tree in java
 
Write a program that displays an AVL tree along with its balance fact.docx
 Write a program that displays an AVL tree along with its balance fact.docx Write a program that displays an AVL tree along with its balance fact.docx
Write a program that displays an AVL tree along with its balance fact.docx
 
Java code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdfJava code to find the closest pair using divide and conquer algorith.pdf
Java code to find the closest pair using divide and conquer algorith.pdf
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
 

Mais de Yoshi Watanabe

Mais de Yoshi Watanabe (8)

Create images with AI models.pptx
Create images with AI models.pptxCreate images with AI models.pptx
Create images with AI models.pptx
 
Git フェッチ
Git フェッチGit フェッチ
Git フェッチ
 
Git リベース
Git リベースGit リベース
Git リベース
 
Git コンフリクト
Git コンフリクトGit コンフリクト
Git コンフリクト
 
Find n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthroughFind n th fibonacci iteratively - illustrated walkthrough
Find n th fibonacci iteratively - illustrated walkthrough
 
Binary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk throughBinary search: illustrated step-by-step walk through
Binary search: illustrated step-by-step walk through
 
Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 
Merge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk throughMerge sort: illustrated step-by-step walk through
Merge sort: illustrated step-by-step walk through
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

Binary search tree exact match - illustrated walkthrough

  • 1. Binary Search Tree - Exact match Illustrated walk through
  • 2. Node structure public class Node { public int Value; public Node Left; public Node Right; } Value Left Right
  • 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. 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