search tree & graph

13
Xproject Tehnical Interview Session January 11, 2013 Michael Jo Mjtoolbox.wordpress.com Binary tree, graph search

Upload: michael-jo

Post on 01-Dec-2014

2.137 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Search tree & graph

Xproject Tehnical Interview Session

January 11, 2013

Michael Jo

Mjtoolbox.wordpress.com

Binary tree, graph search

Page 2: Search tree & graph

• Approach• Trees• Binary Tree• Binary Search Tree• Binary Heap

•Graph• Traversal• Search•BFS•DFS•Questions

Agenda – Trees & Graphs

Page 3: Search tree & graph

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.

Approach

Page 4: Search tree & graph

• Parent

• Child

• Descendent

• Ancestor

• Leaves

• Root (Current node)

• Red-black tree• In-Order search. O(log n)

Trees

Page 5: Search tree & graph

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

Data Structure

Binary Search Tree Binary HeapBinary Tree

Page 6: Search tree & graph

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

Binary Tree

In-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

Page 7: Search tree & graph

• 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

Tree Traversal Exercise

Page 8: Search tree & graph

• Vertices - Nodes

• Edges - Lines

• Directed graph

• Undirected graph

Graph

Directed Graph

Undirected Graph

Page 9: Search tree & graph

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.

Search

Page 10: Search tree & graph

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

http://mjtoolbox.wordpress.com/snippet/tree-traversal

Question 1Implement In-Order traversal of Binary Tree with numbers. One with recursion, one without recursion.

Page 12: Search tree & graph

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

2 3

4 5 6

This demonstrates Breadth First tree traversal algorithm.

http://www.ardendertat.com/2011/12/05/programming-interview-questions-20-tree-level-order-print/

Question 3

Page 13: Search tree & graph

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/

Question 4 (Extra)