Your SlideShare is downloading. ×
Search tree & graph
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Search tree & graph


Published on

Explains the basic concept of Tree and Graph traversal with several technical interview questions.

Explains the basic concept of Tree and Graph traversal with several technical interview questions.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Xproject Tehnical Interview SessionJanuary 11, 2013Michael
  • 2. Agenda – Trees & Graphs• Approach• Trees • Binary Tree • Binary Search Tree • Binary Heap• Graph• Traversal• Search • BFS • DFS• Questions
  • 3. ApproachTrees and graphs questions typically come in one of two forms:1. Implement a tree / find a node / delete a node / other well knownalgorithm.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 StructureBinary TreeBinary Search Tree (Ordered/Sorted Binary Tree)• Left child and descendent value < node itself• Right child and descendent value > node itselfBinary Heap• Each child of node value < node itselfBinary 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, 110In-Order :25, 50, 75, 100, 110, 125, 150, 175Pre-Order :100, 50, 25, 75,150, 125, 110,175Post-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 graphDirected Graph Undirected Graph
  • 9. SearchBreadth 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 1Implement In-Order traversal of Binary Tree with numbers. One withrecursion, one without recursion.
  • 11. Question 2Given a binary tree, check whether it is Binary Search Tree or not.This demonstrates Tree Traversal algorithm.
  • 12. Question 3Given a binary tree of integers, print it in level order. The output will containspace between the numbers in the same level, and new line betweendifferent levels.Output should be :123456This demonstrates Breadth First tree traversal algorithm.
  • 13. Question 4 (Extra)Given the root of a binary search tree and 2 numbers min and max, trim thetree 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, ifwe get this tree as input and min value as 5 and max value as 13, then theresulting binary search tree should be:This will demonstrate tree traversal algorithm and deletion.