SlideShare a Scribd company logo
1 of 1
Download to read offline
I need to create two binary search trees in Ruby. One using recursion. One using iteration. I'm
new to programming so I don't understand the differences between the two and could use some
help getting started with these two programs.
Solution
For recursive:
def insert_rec(node, value)
if node.nil?
return Node.new(value, nil, nil)
elsif value < node.value
node.left ||= insert_rec(node.left, value)
elsif value > node.value
node.right ||= insert_rec(node.right, value)
else
# TODO return?
end
end
for iteration:
class Node
def initialize(name, nodes
@name = name
@nodes = nodes
end
end
class Iterator
def initialize(node)
@node = node
end
def next
???
end
end
In recursion, function call itself until the base condition is reached whereas iteration means
repetition of process until the condition fails.

More Related Content

Similar to I need to create two binary search trees in Ruby. One using recursio.pdf

Functional Objects in Ruby: new horizons – Valentine Ostakh
Functional Objects in Ruby: new horizons  – Valentine OstakhFunctional Objects in Ruby: new horizons  – Valentine Ostakh
Functional Objects in Ruby: new horizons – Valentine OstakhRuby Meditation
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby MetaprogrammingThaichor Seng
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programmingnewmedio
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversBrian Gesiak
 
08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptxcursdjango
 
Fernando Daciuk - The magic world of tests with Jest
Fernando Daciuk - The magic world of tests with JestFernando Daciuk - The magic world of tests with Jest
Fernando Daciuk - The magic world of tests with JestReact Conf Brasil
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Dataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in RubyDataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in RubyLarry Diehl
 

Similar to I need to create two binary search trees in Ruby. One using recursio.pdf (10)

Functional Objects in Ruby: new horizons – Valentine Ostakh
Functional Objects in Ruby: new horizons  – Valentine OstakhFunctional Objects in Ruby: new horizons  – Valentine Ostakh
Functional Objects in Ruby: new horizons – Valentine Ostakh
 
Clojure for Rubyists
Clojure for RubyistsClojure for Rubyists
Clojure for Rubyists
 
Ruby Metaprogramming
Ruby MetaprogrammingRuby Metaprogramming
Ruby Metaprogramming
 
Introduction To Functional Programming
Introduction To Functional ProgrammingIntroduction To Functional Programming
Introduction To Functional Programming
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
Ruby Intro {spection}
Ruby Intro {spection}Ruby Intro {spection}
Ruby Intro {spection}
 
08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx08-Iterators-and-Generators.pptx
08-Iterators-and-Generators.pptx
 
Fernando Daciuk - The magic world of tests with Jest
Fernando Daciuk - The magic world of tests with JestFernando Daciuk - The magic world of tests with Jest
Fernando Daciuk - The magic world of tests with Jest
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Dataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in RubyDataflow: Declarative concurrency in Ruby
Dataflow: Declarative concurrency in Ruby
 

More from Footageetoffe16

Why did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdfWhy did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdfFootageetoffe16
 
Why was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdfWhy was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdfFootageetoffe16
 
What is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdfWhat is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdfFootageetoffe16
 
What were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdfWhat were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdfFootageetoffe16
 
What is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdfWhat is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdfFootageetoffe16
 
What is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdfWhat is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdfFootageetoffe16
 
What are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdfWhat are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdfFootageetoffe16
 
What are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdfWhat are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdfFootageetoffe16
 
The owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdfThe owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdfFootageetoffe16
 
The government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdfThe government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdfFootageetoffe16
 
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdfSyntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdfFootageetoffe16
 
scene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdfscene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdfFootageetoffe16
 
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdfQUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdfFootageetoffe16
 
Problems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdfProblems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdfFootageetoffe16
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfFootageetoffe16
 
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdfPLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdfFootageetoffe16
 
List the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdfList the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdfFootageetoffe16
 
Information is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdfInformation is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdfFootageetoffe16
 
I am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdfI am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdfFootageetoffe16
 
httpswww.aitp.orgnews98451 Security Administration and Acces.pdf
httpswww.aitp.orgnews98451 Security Administration and Acces.pdfhttpswww.aitp.orgnews98451 Security Administration and Acces.pdf
httpswww.aitp.orgnews98451 Security Administration and Acces.pdfFootageetoffe16
 

More from Footageetoffe16 (20)

Why did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdfWhy did the defendants fight jurisdiction in the United States in th.pdf
Why did the defendants fight jurisdiction in the United States in th.pdf
 
Why was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdfWhy was the frontier important in American historySolutionFro.pdf
Why was the frontier important in American historySolutionFro.pdf
 
What is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdfWhat is the multiplication principle Also, provide example.Solu.pdf
What is the multiplication principle Also, provide example.Solu.pdf
 
What were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdfWhat were the three primary goals of the FASB in developing the Codi.pdf
What were the three primary goals of the FASB in developing the Codi.pdf
 
What is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdfWhat is the difference between public relations and public affairs.pdf
What is the difference between public relations and public affairs.pdf
 
What is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdfWhat is Network Chatter and how does it affect performanceSolut.pdf
What is Network Chatter and how does it affect performanceSolut.pdf
 
What are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdfWhat are the elements types to add video and audio and give some att.pdf
What are the elements types to add video and audio and give some att.pdf
 
What are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdfWhat are the types of nonverbal communication used by President John.pdf
What are the types of nonverbal communication used by President John.pdf
 
The owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdfThe owner of a small business is planning on expanding his business. .pdf
The owner of a small business is planning on expanding his business. .pdf
 
The government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdfThe government safety net creates an adverse selection problem and m.pdf
The government safety net creates an adverse selection problem and m.pdf
 
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdfSyntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
Syntrophy often involvesA. hydrogenB.OxygenC. fermentation by.pdf
 
scene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdfscene from American Samoa Paradise Lost •How does power, both form.pdf
scene from American Samoa Paradise Lost •How does power, both form.pdf
 
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdfQUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
QUESTION 25 Chris Korda, an advocate of a certain religion, publishes.pdf
 
Problems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdfProblems2. This is a map for a diploid plantR--------35-------.pdf
Problems2. This is a map for a diploid plantR--------35-------.pdf
 
Prompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdfPrompt a user to enter a series of integers separated by spaces and .pdf
Prompt a user to enter a series of integers separated by spaces and .pdf
 
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdfPLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
PLEASE PLEASE use you own words, do not copy and paste !!!This is .pdf
 
List the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdfList the limitations of the GDP. Which one is most importantSol.pdf
List the limitations of the GDP. Which one is most importantSol.pdf
 
Information is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdfInformation is given about a polynomial f(x) whose coefficients are r.pdf
Information is given about a polynomial f(x) whose coefficients are r.pdf
 
I am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdfI am studying gut flora and had a few questions.Can you discribe a.pdf
I am studying gut flora and had a few questions.Can you discribe a.pdf
 
httpswww.aitp.orgnews98451 Security Administration and Acces.pdf
httpswww.aitp.orgnews98451 Security Administration and Acces.pdfhttpswww.aitp.orgnews98451 Security Administration and Acces.pdf
httpswww.aitp.orgnews98451 Security Administration and Acces.pdf
 

Recently uploaded

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 

Recently uploaded (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

I need to create two binary search trees in Ruby. One using recursio.pdf

  • 1. I need to create two binary search trees in Ruby. One using recursion. One using iteration. I'm new to programming so I don't understand the differences between the two and could use some help getting started with these two programs. Solution For recursive: def insert_rec(node, value) if node.nil? return Node.new(value, nil, nil) elsif value < node.value node.left ||= insert_rec(node.left, value) elsif value > node.value node.right ||= insert_rec(node.right, value) else # TODO return? end end for iteration: class Node def initialize(name, nodes @name = name @nodes = nodes end end class Iterator def initialize(node) @node = node end def next ??? end end In recursion, function call itself until the base condition is reached whereas iteration means repetition of process until the condition fails.