SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

重構—改善既有程式的設計(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
 

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von Yoshi Watanabe

Mehr von 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
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 

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