SlideShare una empresa de Scribd logo
1 de 13
Xproject Tehnical Interview Session
January 11, 2013
Michael Jo
Mjtoolbox.wordpress.com
Agenda – Trees & Graphs
• Approach
• Trees
 • Binary Tree
 • Binary Search Tree
 • Binary Heap
• Graph
• Traversal
• Search
  • BFS
  • DFS
• Questions
Approach
Trees and graphs questions typically come in one of two forms:
1. Implement a tree / find a node / delete a node / other well known
algorithm.
2. Implement a modification of a known algorithm.
Trees
• Parent
• Child
• Descendent
• Ancestor
• Leaves
• Root (Current node)
• Red-black tree
 • In-Order search. O(log n)
Data Structure
Binary Tree
Binary Search Tree (Ordered/Sorted Binary Tree)
• Left child and descendent value < node itself
• Right child and descendent value > node itself
Binary Heap
• Each child of node value < node itself




Binary Tree                Binary Search Tree      Binary Heap
Binary Tree
 Traversal
 • Depth First traversal
   • In-Order : Left child, Root, Right child (Used in BST)
   • Pre-Order : Root, Left child, Right child
   • Post-Order : Left child, Right child, Root

 • Breadth First traversal
   • Not for large tree. O(n). Memory intense.
   • 100, 50, 150, 25, 75, 125, 175, 110



In-Order :
25, 50, 75, 100, 110, 125, 150, 175
Pre-Order :
100, 50, 25, 75,150, 125, 110,175
Post-Order :
25, 75, 50, 110, 125, 175, 150
Tree Traversal Exercise
• In-Order traversal sequence (left, root, right) :
   A, B, C, D, E, F, G, H, I

• Pre-Order traversal sequence (root, left, right) :
   F, B, A, D, C, E, G, I, H

• Post-Order traversal sequence (left, right, root) :
   A, C, E, D, B, H, I, G, F
Graph
• Vertices - Nodes
• Edges - Lines
• Directed graph
• Undirected graph




Directed Graph
                     Undirected Graph
Search
Breadth First Search (BFS)
•   Searching a node and all its children before proceeding to its siblings.
•   Use Queue data structure as an implementation.

Depth First Search (DFS)
•   Searching a node and its siblings before going on to any children.
•   Use Stack data structure as an implementation.



• Tip 1 : Decide your search algorithm based on the scenario.
• Tip 2 : Avoid BFS in a large tree.
Question 1
Implement In-Order traversal of Binary Tree with numbers. One with
recursion, one without recursion.

http://youtu.be/2lxVhW5-GTk


http://mjtoolbox.wordpress.com/snippet/tree-traversal
Question 2
Given a binary tree, check whether it is Binary Search Tree or not.
This demonstrates Tree Traversal algorithm.

http://www.ardendertat.com/2011/10/10/programming-
interview-questions-7-binary-search-tree-check/
http://mjtoolbox.wordpress.com/snippet/tree-
traversal
Question 3
Given a binary tree of integers, print it in level order. The output will contain
space between the numbers in the same level, and new line between
different levels.
Output should be :
1
23
456
This demonstrates Breadth First tree traversal algorithm.
http://www.ardendertat.com/2011/12/05/programming-interview-questions-
20-tree-level-order-print/
Question 4 (Extra)
Given the root of a binary search tree and 2 numbers min and max, trim the
tree such that all the numbers in the new tree are between min and max
(inclusive). The resulting tree should still be a valid binary search tree. So, if
we get this tree as input and min value as 5 and max value as 13, then the
resulting binary search tree should be:
This will demonstrate tree traversal algorithm and deletion.




http://www.ardendertat.com/2012/01/17/programming-interview-questions-
26-trim-binary-search-tree/

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Trees
TreesTrees
Trees
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
Introduction to Data Structure part 1
Introduction to Data Structure part 1Introduction to Data Structure part 1
Introduction to Data Structure part 1
 
Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]Data Structures - Lecture 2 [Introduction to Data Structures]
Data Structures - Lecture 2 [Introduction to Data Structures]
 
Types Of Data Structure
Types Of Data StructureTypes Of Data Structure
Types Of Data Structure
 
Introduction To Data Structures.
Introduction To Data Structures.Introduction To Data Structures.
Introduction To Data Structures.
 
Data Structure # vpmp polytechnic
Data Structure # vpmp polytechnicData Structure # vpmp polytechnic
Data Structure # vpmp polytechnic
 
Implementation of trees
Implementation of trees Implementation of trees
Implementation of trees
 
Files and data storage
Files and data storageFiles and data storage
Files and data storage
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Trees krw
Trees krwTrees krw
Trees krw
 
Basic of trees 2
Basic of trees 2Basic of trees 2
Basic of trees 2
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
Introduction to Data Structures
Introduction to Data StructuresIntroduction to Data Structures
Introduction to Data Structures
 
1.introduction to data_structures
1.introduction to data_structures1.introduction to data_structures
1.introduction to data_structures
 
Tree(Directed and undirected tree)
Tree(Directed and undirected tree)Tree(Directed and undirected tree)
Tree(Directed and undirected tree)
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
R Data Structures (Part 1)
R Data Structures (Part 1)R Data Structures (Part 1)
R Data Structures (Part 1)
 

Similar a Search tree & graph

Lecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itLecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk it
AmazingWorld37
 

Similar a Search tree & graph (20)

Hub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data StructureHub102 - Lesson4 - Data Structure
Hub102 - Lesson4 - Data Structure
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Binary tree
Binary treeBinary tree
Binary tree
 
Data Science 101
Data Science 101Data Science 101
Data Science 101
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Tree Traversal #SalesforceSaturday
Tree Traversal #SalesforceSaturdayTree Traversal #SalesforceSaturday
Tree Traversal #SalesforceSaturday
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
unit 4 for trees data structure notes it is
unit 4 for trees data structure notes it isunit 4 for trees data structure notes it is
unit 4 for trees data structure notes it is
 
How to choose a database
How to choose a databaseHow to choose a database
How to choose a database
 
Lecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk itLecture 09 - Binary Search Trees.pptx mission Sk it
Lecture 09 - Binary Search Trees.pptx mission Sk it
 
CM20315_01_Intro_Machine_Learning_ap.pptx
CM20315_01_Intro_Machine_Learning_ap.pptxCM20315_01_Intro_Machine_Learning_ap.pptx
CM20315_01_Intro_Machine_Learning_ap.pptx
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
Mastering the 80% of Analytics: What Data Scientists Really Do
Mastering the 80% of Analytics: What Data Scientists Really DoMastering the 80% of Analytics: What Data Scientists Really Do
Mastering the 80% of Analytics: What Data Scientists Really Do
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Creating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxCreating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptx
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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
 
[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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
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...
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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
 
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
 
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
 
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
 
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
 

Search tree & graph

  • 1. Xproject Tehnical Interview Session January 11, 2013 Michael Jo Mjtoolbox.wordpress.com
  • 2. Agenda – Trees & Graphs • Approach • Trees • Binary Tree • Binary Search Tree • Binary Heap • Graph • Traversal • Search • BFS • DFS • Questions
  • 3. Approach Trees and graphs questions typically come in one of two forms: 1. Implement a tree / find a node / delete a node / other well known algorithm. 2. Implement a modification of a known algorithm.
  • 4. Trees • Parent • Child • Descendent • Ancestor • Leaves • Root (Current node) • Red-black tree • In-Order search. O(log n)
  • 5. Data Structure Binary Tree Binary Search Tree (Ordered/Sorted Binary Tree) • Left child and descendent value < node itself • Right child and descendent value > node itself Binary Heap • Each child of node value < node itself Binary Tree Binary Search Tree Binary Heap
  • 6. Binary Tree Traversal • Depth First traversal • In-Order : Left child, Root, Right child (Used in BST) • Pre-Order : Root, Left child, Right child • Post-Order : Left child, Right child, Root • Breadth First traversal • Not for large tree. O(n). Memory intense. • 100, 50, 150, 25, 75, 125, 175, 110 In-Order : 25, 50, 75, 100, 110, 125, 150, 175 Pre-Order : 100, 50, 25, 75,150, 125, 110,175 Post-Order : 25, 75, 50, 110, 125, 175, 150
  • 7. Tree Traversal Exercise • In-Order traversal sequence (left, root, right) : A, B, C, D, E, F, G, H, I • Pre-Order traversal sequence (root, left, right) : F, B, A, D, C, E, G, I, H • Post-Order traversal sequence (left, right, root) : A, C, E, D, B, H, I, G, F
  • 8. Graph • Vertices - Nodes • Edges - Lines • Directed graph • Undirected graph Directed Graph Undirected Graph
  • 9. Search Breadth First Search (BFS) • Searching a node and all its children before proceeding to its siblings. • Use Queue data structure as an implementation. Depth First Search (DFS) • Searching a node and its siblings before going on to any children. • Use Stack data structure as an implementation. • Tip 1 : Decide your search algorithm based on the scenario. • Tip 2 : Avoid BFS in a large tree.
  • 10. Question 1 Implement In-Order traversal of Binary Tree with numbers. One with recursion, one without recursion. http://youtu.be/2lxVhW5-GTk http://mjtoolbox.wordpress.com/snippet/tree-traversal
  • 11. Question 2 Given a binary tree, check whether it is Binary Search Tree or not. This demonstrates Tree Traversal algorithm. http://www.ardendertat.com/2011/10/10/programming- interview-questions-7-binary-search-tree-check/ http://mjtoolbox.wordpress.com/snippet/tree- traversal
  • 12. Question 3 Given a binary tree of integers, print it in level order. The output will contain space between the numbers in the same level, and new line between different levels. Output should be : 1 23 456 This demonstrates Breadth First tree traversal algorithm. http://www.ardendertat.com/2011/12/05/programming-interview-questions- 20-tree-level-order-print/
  • 13. Question 4 (Extra) Given the root of a binary search tree and 2 numbers min and max, trim the tree such that all the numbers in the new tree are between min and max (inclusive). The resulting tree should still be a valid binary search tree. So, if we get this tree as input and min value as 5 and max value as 13, then the resulting binary search tree should be: This will demonstrate tree traversal algorithm and deletion. http://www.ardendertat.com/2012/01/17/programming-interview-questions- 26-trim-binary-search-tree/