Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course
Binary Search Trees <ul><li>A binary tree is a tree where each node has at most two children, referred to as the left and ...
Problems <ul><li>Problem 1: </li></ul><ul><li>How do you sort n numbers using a binary  </li></ul><ul><li>Search tree ?? W...
Binary Search Tree properties <ul><li>All dynamic-set operations (Search, Insert, Delete, Min, Max, successor, predecessor...
Balance of Binary Trees <ul><li>A binary tree can be balanced, such that one branch of the tree is about the same size and...
Sorting  Using BST  <ul><li>Inorder traversal of a binary search tree always gives a sorted  </li></ul><ul><li>sequence of...
Example Sorting Using BST  <ul><li>Input Sequence  :-  2  3  8  4  5  7  6  1  2.5  2.4 </li></ul><ul><li>Step 1 : Creatin...
Example Sorting Using BST (cont.) <ul><li>Input Sequence  :-  2  3  8  4  5  7  6  1  2.5  2.4 </li></ul><ul><li>Step 2 : ...
Example Sorting Using BST (cont.) <ul><li>Input Sequence  :-  2  3  8  4  5  7  6  1  2.5  2.4 </li></ul><ul><li>Step 2 : ...
Example Sorting Using BST (cont.)  <ul><li>Input Sequence  :-  2  3  8  4  5  7  6  1  2.5  2.4 </li></ul><ul><li>Step 2 :...
Worst cases <ul><li>Input Sequence  :-  8 4 3 2 and 2 3 8 10 </li></ul><ul><li>Step 1 : Creating Binary Search Tree  of ab...
Finding Min & Max <ul><li>Tree-Minimum( x )     Tree-Maximum( x ) </li></ul><ul><li>1.  while   left [ x ]     NIL   1.  ...
Algorithm LCA (Node v, Node w): <ul><ul><ul><li>int vdpth    v.depth </li></ul></ul></ul><ul><ul><ul><li>int wdpth    w....
<ul><li>Give an efficient algorithm for converting an infix arithmetic expression into its equivalent postfix notation ? <...
 
Algorithm buildExpressionTree (E): <ul><li>Input : Fully-parenthesized arithmetic Expression </li></ul><ul><li>Output : A ...
Algorithm PostFix (E): <ul><li>Input: Full-Parenthesized arithmetic Expression ,E. </li></ul><ul><li>Output: PostFix notat...
Representing Graphs <ul><li>Assume V = {1, 2, …,  n } </li></ul><ul><li>An  adjacency matrix   represents the graph as a  ...
Graphs: Adjacency Matrix <ul><li>Example: </li></ul>1 2 4 3 a d b c How much storage does the adjacency matrix require? A:...
Graphs: Adjacency List <ul><li>Adjacency list: for each vertex  v    V, store a list of vertices adjacent to  v </li></ul...
Graph Problems <ul><li>Given an adjacency-list representation of a directed graph, how long does it take to compute the ou...
Solution <ul><li>Given the adjacency-Matrix representation of a graph the out and in-degree of every node can easily be co...
Solution <ul><li>Given the adjacency-List representation of a graph the out and in-degree of every node can easily be comp...
Problem <ul><li>The transpose of a directed graph G = (V,E) is the graph G with all its edges reversed. Describe efficient...
Solution <ul><li>Given the adjacency-Matrix representation of a graph the transpose can be computed as follows. </li></ul>...
Solution <ul><li>Given the adjacency-List representation of a graph the transpose can be computed as follows. </li></ul><u...
Predecessor and Successor in a BST <ul><li>Successor of node  x  is the  node  y  such that  key [ y ] is the smallest key...
Pseudo-code for Successor <ul><li>Tree-Successor( x ) </li></ul><ul><li>if  right [ x ]     NIL  </li></ul><ul><li>2.  th...
Properties of Binary Trees <ul><li>Note that external nodes are also called leaf nodes </li></ul><ul><li>Let  </li></ul><u...
Upcoming SlideShare
Loading in …5
×

Review session2

293 views
239 views

Published on

jst for info

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
293
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Review session2

  1. 1. Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course
  2. 2. Binary Search Trees <ul><li>A binary tree is a tree where each node has at most two children, referred to as the left and right child </li></ul><ul><li>A binary search tree is a binary tree where every node's left subtree holds values less than the node's value, and every right subtree holds values greater. </li></ul><ul><li>A new node is added as a leaf. </li></ul>parent left child right child root 17 11 19
  3. 3. Problems <ul><li>Problem 1: </li></ul><ul><li>How do you sort n numbers using a binary </li></ul><ul><li>Search tree ?? What are the best, worst and </li></ul><ul><li>Average case time complexities ?? </li></ul>
  4. 4. Binary Search Tree properties <ul><li>All dynamic-set operations (Search, Insert, Delete, Min, Max, successor, predecessor) can be supported in O ( h ) time . </li></ul><ul><li>h =  ( lg n ) for a balanced binary tree (and for an average tree built by adding nodes in random order.) </li></ul><ul><li>h =  ( n ) for an unbalanced tree that resembles a linear chain of n nodes in the worst case. </li></ul><ul><li>Red-black trees are a variation of binary search trees to ensure that the tree is balanced . </li></ul><ul><ul><li>Height is O (log n ), where n is the number of nodes. </li></ul></ul>
  5. 5. Balance of Binary Trees <ul><li>A binary tree can be balanced, such that one branch of the tree is about the same size and depth as the other. </li></ul><ul><li>In order to find out if a tree node is balanced, you need to find out the maximum height level of both children in each node, and if they differ by more than one level, it is considered unbalanced. </li></ul><ul><li>If the number is -1, 0, or 1, the node is balanced. If the difference is anything else, then it is unbalanced. Note that a balanced binary tree requires every node in the tree to have the balanced property. </li></ul>
  6. 6. Sorting Using BST <ul><li>Inorder traversal of a binary search tree always gives a sorted </li></ul><ul><li>sequence of the values. This is a direct consequence of the BST </li></ul><ul><li>property. </li></ul><ul><li>Given a set of unordered elements, the following method can be </li></ul><ul><li>used to Sort the elements: </li></ul><ul><ul><li>construct a binary search tree whose keys are those elements, and then </li></ul></ul><ul><ul><li>perform an inorder traversal of this tree. </li></ul></ul><ul><li>BSTSort(A) 1. for each element in an array do 2. Insert element in the BST// Constructing a BST take O( log n) time 3. Inorder-Traversal (root) // Takes O(n) time </li></ul><ul><li>Best case running time of BSTSort(A) is O( n log n). </li></ul><ul><li>Worst case running time is O(n 2 ) since each inserting could take O(n) time in worst case. </li></ul>
  7. 7. Example Sorting Using BST <ul><li>Input Sequence :- 2 3 8 4 5 7 6 1 2.5 2.4 </li></ul><ul><li>Step 1 : Creating Binary Search Tree of above given input sequence. </li></ul>2 1 3 4 8 6 5 7 2.4 2.5
  8. 8. Example Sorting Using BST (cont.) <ul><li>Input Sequence :- 2 3 8 4 5 7 6 1 2.5 2.4 </li></ul><ul><li>Step 2 : Perform Inorder-Traversal. </li></ul>2 1 3 4 8 6 5 7 2.4 2.5 2 1
  9. 9. Example Sorting Using BST (cont.) <ul><li>Input Sequence :- 2 3 8 4 5 7 6 1 2.5 2.4 </li></ul><ul><li>Step 2 : Perform Inorder-Traversal. </li></ul>2 1 3 4 8 6 5 7 2.4 2.5 2 1 2.4 2.5 3
  10. 10. Example Sorting Using BST (cont.) <ul><li>Input Sequence :- 2 3 8 4 5 7 6 1 2.5 2.4 </li></ul><ul><li>Step 2 : Perform Inorder-Traversal. </li></ul>2 1 3 4 8 6 5 7 2.4 2.5 2 1 2.4 2.5 3 4 5 6 7 8 Sorted Array
  11. 11. Worst cases <ul><li>Input Sequence :- 8 4 3 2 and 2 3 8 10 </li></ul><ul><li>Step 1 : Creating Binary Search Tree of above given input </li></ul><ul><li>sequence. </li></ul>2 3 10 8 2 3 4 8
  12. 12. Finding Min & Max <ul><li>Tree-Minimum( x ) Tree-Maximum( x ) </li></ul><ul><li>1. while left [ x ]  NIL 1. while right [ x ]  NIL </li></ul><ul><li>2. do x  left [ x ] 2. do x  right [ x ] </li></ul><ul><li>3. return x 3. return x </li></ul>Q: How long do they take? <ul><li>The binary-search-tree property guarantees that: </li></ul><ul><ul><li>The minimum is located at the left-most node. </li></ul></ul><ul><ul><li>The maximum is located at the right-most node. </li></ul></ul>
  13. 13. Algorithm LCA (Node v, Node w): <ul><ul><ul><li>int vdpth  v.depth </li></ul></ul></ul><ul><ul><ul><li>int wdpth  w.depth </li></ul></ul></ul><ul><ul><ul><li>while vdpth > wdpth do </li></ul></ul></ul><ul><ul><ul><li>v  v.parent </li></ul></ul></ul><ul><ul><ul><li>vdpth  vdpth -1 </li></ul></ul></ul><ul><ul><ul><li>end while </li></ul></ul></ul><ul><ul><ul><li>while wdpth > vdpth do </li></ul></ul></ul><ul><ul><ul><li>w  w.parent </li></ul></ul></ul><ul><ul><ul><li>wdpth  wdpth -1 </li></ul></ul></ul><ul><ul><ul><li>end while </li></ul></ul></ul><ul><ul><ul><li>while v ≠ w do </li></ul></ul></ul><ul><ul><ul><li>v  v.parent </li></ul></ul></ul><ul><ul><ul><li>w  w.parent </li></ul></ul></ul><ul><ul><ul><li>end while </li></ul></ul></ul><ul><ul><ul><li>return v </li></ul></ul></ul>Note that LCA algorithm is applicable for any tree
  14. 14. <ul><li>Give an efficient algorithm for converting an infix arithmetic expression into its equivalent postfix notation ? </li></ul><ul><li>(Hint: First convert the infix expression into its equivalent binary tree representation) </li></ul>
  15. 16. Algorithm buildExpressionTree (E): <ul><li>Input : Fully-parenthesized arithmetic Expression </li></ul><ul><li>Output : A binary Tree T representing Expression </li></ul><ul><li>S  a new empty stack </li></ul><ul><li>For i  0 to n-1 do { </li></ul><ul><li>if E[i] is a variable or an operator then </li></ul><ul><li>T  new binary tree with E[i] as root </li></ul><ul><li>S.push(T) </li></ul><ul><li>else if E[i] = “(“ then continue; </li></ul><ul><li>else if E[i] = “)” </li></ul><ul><li>T2  S.pop() </li></ul><ul><li>T  S.pop() </li></ul><ul><li>T1  S.pop() </li></ul><ul><li>Attach T1 as T’s left subtree and T2 as its right subtree </li></ul><ul><li>S.push(T) </li></ul><ul><li>} </li></ul><ul><li>Return S.pop() </li></ul>
  16. 17. Algorithm PostFix (E): <ul><li>Input: Full-Parenthesized arithmetic Expression ,E. </li></ul><ul><li>Output: PostFix notation of E </li></ul><ul><li>T  buildExpressionTree (E) </li></ul><ul><li>Postorder-Traversal (T.root) // this gives the </li></ul><ul><li>Postfix notation of the expression </li></ul><ul><li>Preorder-Traversal (T.root) // this gives the </li></ul><ul><li>Prefix notation of the expression </li></ul><ul><li>Inorder-Traversal (T.root) // this gives the </li></ul><ul><li>Infix notation of the expression </li></ul>
  17. 18. Representing Graphs <ul><li>Assume V = {1, 2, …, n } </li></ul><ul><li>An adjacency matrix represents the graph as a n x n matrix A: </li></ul><ul><ul><li>A[ i , j ] = 1 if edge ( i , j )  E (or weight of edge) = 0 if edge ( i , j )  E </li></ul></ul>
  18. 19. Graphs: Adjacency Matrix <ul><li>Example: </li></ul>1 2 4 3 a d b c How much storage does the adjacency matrix require? A: O(V 2 ) 0 1 0 0 4 0 0 0 0 3 0 1 0 0 2 0 1 1 0 1 4 3 2 1 A
  19. 20. Graphs: Adjacency List <ul><li>Adjacency list: for each vertex v  V, store a list of vertices adjacent to v </li></ul><ul><li>Example: </li></ul><ul><ul><li>Adj[1] = {2,3} </li></ul></ul><ul><ul><li>Adj[2] = {3} </li></ul></ul><ul><ul><li>Adj[3] = {} </li></ul></ul><ul><ul><li>Adj[4] = {3} </li></ul></ul><ul><li>Variation: can also keep a list of edges coming into vertex </li></ul>1 2 4 3 <ul><li>Adjacency lists take O(V+E) storage </li></ul>
  20. 21. Graph Problems <ul><li>Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree and in-degree of every vertex ? Also, how long is it going to take if we use the adjacency matrix instead ? </li></ul>
  21. 22. Solution <ul><li>Given the adjacency-Matrix representation of a graph the out and in-degree of every node can easily be computed as follows. </li></ul><ul><li>For I  1 to v // v vertices </li></ul><ul><li>For J  1 to v // v vertices </li></ul><ul><li>if(A[I][J] == 1) then </li></ul><ul><li>outdegree[I]++; </li></ul><ul><li>indegree[J]++; </li></ul><ul><li>Time complexity : O(V 2 ) </li></ul>
  22. 23. Solution <ul><li>Given the adjacency-List representation of a graph the out and in-degree of every node can easily be computed as follows. </li></ul><ul><li>For I  1 to v // n vertices </li></ul><ul><li>For J  1 to A[I].size() // neighbours of vertex I </li></ul><ul><li>n  A[i].get(J) </li></ul><ul><li>outdegree[I]++; </li></ul><ul><li>indegree[n]++; </li></ul><ul><li>Time complexity : O(V+E) </li></ul>
  23. 24. Problem <ul><li>The transpose of a directed graph G = (V,E) is the graph G with all its edges reversed. Describe efficient algorithms for computing the transpose of G for both adjacency list and matrix representations. What are the running times ? </li></ul>
  24. 25. Solution <ul><li>Given the adjacency-Matrix representation of a graph the transpose can be computed as follows. </li></ul><ul><li>For I  1 to v // v vertices </li></ul><ul><li>For J  1 to v // v vertices </li></ul><ul><li>Transpose[I][J] = A[J][I] </li></ul><ul><li>Time complexity : O(V 2 ) </li></ul>
  25. 26. Solution <ul><li>Given the adjacency-List representation of a graph the transpose can be computed as follows. </li></ul><ul><li>For I  1 to n // n vertices </li></ul><ul><li>For J  1 to A[I].size() // neighbours of vertex I </li></ul><ul><li>Transpose[I].add(A[J].get(I)) </li></ul><ul><li>Time complexity : O(V+E) </li></ul>
  26. 27. Predecessor and Successor in a BST <ul><li>Successor of node x is the node y such that key [ y ] is the smallest key greater than key [ x ] . </li></ul><ul><li>The successor of the largest key is NIL. </li></ul><ul><li>Search consists of two cases. </li></ul><ul><ul><li>If node x has a non-empty right subtree , then x ’s successor is the minimum in the right subtree of x . </li></ul></ul><ul><ul><li>If node x has an empty right subtree , then: </li></ul></ul><ul><ul><ul><li>As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. </li></ul></ul></ul><ul><ul><ul><li>x ’s successor y is the node that x is the predecessor of ( x is the maximum in y ’s left subtree). </li></ul></ul></ul><ul><ul><ul><li>In other words, x ’s successor y , is the lowest ancestor of x whose left child is also an ancestor of x . </li></ul></ul></ul>
  27. 28. Pseudo-code for Successor <ul><li>Tree-Successor( x ) </li></ul><ul><li>if right [ x ]  NIL </li></ul><ul><li>2. then return Tree-Minimum( right [ x ]) </li></ul><ul><li>3. y  p [ x ] </li></ul><ul><li>4. while y  NIL and x = right [ y ] </li></ul><ul><li>5. do x  y </li></ul><ul><li>6. y  p [ y ] </li></ul><ul><li>7. return y </li></ul>Code for predecessor is symmetric. Running time: O ( h ) 56 26 200 18 28 190 213 12 24 27
  28. 29. Properties of Binary Trees <ul><li>Note that external nodes are also called leaf nodes </li></ul><ul><li>Let </li></ul><ul><li># leaf nodes = E, # internal nodes = I , Height = H, # nodes = N, </li></ul><ul><li>E = I + 1 </li></ul><ul><li>N = E + I </li></ul><ul><li>(# nodes at level x) <= 2 x </li></ul><ul><li>E <= 2 H </li></ul><ul><li>H >= log 2 I </li></ul><ul><li>A full binary tree has (2 H+1 – 1) nodes. </li></ul><ul><li>(H+1) ≤ E ≤ 2 H . </li></ul><ul><li>H≤ I ≤ 2 H -1. </li></ul><ul><li>2H+1 ≤ N ≤ 2 H -1. </li></ul><ul><li>log 2 (N+1)-1 ≤ H ≤ (N-1)/2 </li></ul><ul><li>log 2 (E) ≤ H ≤ E-1 </li></ul><ul><li>log 2 (I+1) ≤ H ≤ I </li></ul>

×