CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.

Slides:



Advertisements
Similar presentations
Chapter 12 Binary Search Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
UNC Chapel Hill Lin/Foskey/Manocha Binary Search Tree Her bir node u bir object olan bir linked data structure ile temsil edilebilir. Her bir node key,
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
DAST 2005 Tirgul 7 Binary Search Trees. DAST 2005 Motivation We would like to have a dynamic ADT that efficiently supports the following common operations:
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Binary Search Tree Qamar Abbas.
Prepared by- Jatinder Paul, Shraddha Rumade
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Trees By P.Naga Srinivasu M.tech,(MBA). Basic Tree Concepts A tree consists of finite set of elements, called nodes, and a finite set of directed lines.
CISC 235 Topic 3 General Trees, Binary Trees, Binary Search Trees.
Binary Search Trees (BST)
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Lecture 91 Data Structures, Algorithms & Complexity Insertion and Deletion in BST GRIFFITH COLLEGE DUBLIN.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Binary Search Trees What is a binary search tree?
DAST Tirgul 7.
Trees Chapter 15.
Binary Search Trees.
Data Structures Review Session 2
CS200: Algorithms Analysis
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Ch. 12: Binary Search Trees Ming-Te Chi
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Topic 6: Binary Search Tree Data structure Operations
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Presentation transcript:

CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course

CS 307 Fundamentals of Computer Science 2 Binary Search Trees  A binary tree is a tree where each node has at most two children, referred to as the left and right child  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.  A new node is added as a leaf. parent left child right child root

CS 307 Fundamentals of Computer Science 3 Problems Problem 1: How do you sort n numbers using a binary Search tree ?? What are the best, worst and Average case time complexities ??

CS 307 Fundamentals of Computer Science 4 Binary Search Tree properties  All dynamic-set operations (Search, Insert, Delete, Min, Max, successor, predecessor) can be supported in O(h) time.  h =  (lg n) for a balanced binary tree (and for an average tree built by adding nodes in random order.)  h =  (n) for an unbalanced tree that resembles a linear chain of n nodes in the worst case.  Red-black trees are a variation of binary search trees to ensure that the tree is balanced.  Height is O (log n), where n is the number of nodes.

CS 307 Fundamentals of Computer Science 5 Balance of Binary Trees  A binary tree can be balanced, such that one branch of the tree is about the same size and depth as the other.  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.  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.

CS 307 Fundamentals of Computer Science 6 Sorting Using BST Inorder traversal of a binary search tree always gives a sorted sequence of the values. This is a direct consequence of the BST property. Given a set of unordered elements, the following method can be used to Sort the elements:  construct a binary search tree whose keys are those elements, and then  perform an inorder traversal of this tree. 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 Best case running time of BSTSort(A) is O( n log n). Worst case running time is O(n 2 ) since each inserting could take O(n) time in worst case.

CS 307 Fundamentals of Computer Science 7 Example Sorting Using BST Input Sequence : Step 1 : Creating Binary Search Tree of above given input sequence

CS 307 Fundamentals of Computer Science 8 Example Sorting Using BST (cont.) Input Sequence : Step 2 : Perform Inorder-Traversal

CS 307 Fundamentals of Computer Science 9 Example Sorting Using BST (cont.) Input Sequence : Step 2 : Perform Inorder-Traversal

CS 307 Fundamentals of Computer Science 10 Example Sorting Using BST (cont.) Input Sequence : Step 2 : Perform Inorder-Traversal Sorted Array

CS 307 Fundamentals of Computer Science 11 Worst cases Input Sequence : and Step 1 : Creating Binary Search Tree of above given input sequence

CS 307 Fundamentals of Computer Science 12 Finding Min & Max Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x Tree-Minimum(x) Tree-Maximum(x) 1. while left[x]  NIL 1. while right[x]  NIL 2. do x  left[x] 2. do x  right[x] 3. return x Q: How long do they take?  The binary-search-tree property guarantees that: » The minimum is located at the left-most node. » The maximum is located at the right-most node.

CS 307 Fundamentals of Computer Science 13 Algorithm LCA (Node v, Node w): int vdpth  v.depth int wdpth  w.depth while vdpth > wdpth do v  v.parent vdpth  vdpth -1 end while while wdpth > vdpth do w  w.parent wdpth  wdpth -1 end while while v ≠ w do v  v.parent w  w.parent end while return v Note that LCA algorithm is applicable for any tree

CS 307 Fundamentals of Computer Science 14 1.Give an efficient algorithm for converting an infix arithmetic expression into its equivalent postfix notation ? (Hint: First convert the infix expression into its equivalent binary tree representation)

CS 307 Fundamentals of Computer Science 15

CS 307 Fundamentals of Computer Science 16 Algorithm buildExpressionTree (E): Input: Fully-parenthesized arithmetic Expression Output: A binary Tree T representing Expression S  a new empty stack For i  0 to n-1 do { if E[i] is a variable or an operator then T  new binary tree with E[i] as root S.push(T) else if E[i] = “(“ then continue; else if E[i] = “)” T2  S.pop() T  S.pop() T1  S.pop() Attach T1 as T’s left subtree and T2 as its right subtree S.push(T) } Return S.pop()

CS 307 Fundamentals of Computer Science 17 Algorithm PostFix (E): Input: Full-Parenthesized arithmetic Expression,E. Output: PostFix notation of E T  buildExpressionTree (E) Postorder-Traversal (T.root) // this gives the Postfix notation of the expression Preorder-Traversal (T.root) // this gives the Prefix notation of the expression Inorder-Traversal (T.root) // this gives the Infix notation of the expression

CS 307 Fundamentals of Computer Science 18 Representing Graphs  Assume V = {1, 2, …, n}  An adjacency matrix represents the graph as a n x n matrix A: –A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E

CS 307 Fundamentals of Computer Science 19 Graphs: Adjacency Matrix  Example: a d bc A How much storage does the adjacency matrix require? A: O(V 2 )

CS 307 Fundamentals of Computer Science 20 Graphs: Adjacency List  Adjacency list: for each vertex v  V, store a list of vertices adjacent to v  Example: –Adj[1] = {2,3} –Adj[2] = {3} –Adj[3] = {} –Adj[4] = {3}  Variation: can also keep a list of edges coming into vertex  Adjacency lists take O(V+E) storage

CS 307 Fundamentals of Computer Science 21 Graph Problems  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 ?

CS 307 Fundamentals of Computer Science 22 Solution  Given the adjacency-Matrix representation of a graph the out and in-degree of every node can easily be computed as follows. For I  1 to v // v vertices For J  1 to v // v vertices if(A[I][J] == 1) then outdegree[I]++; indegree[J]++; Time complexity : O(V 2 )

CS 307 Fundamentals of Computer Science 23 Solution  Given the adjacency-List representation of a graph the out and in-degree of every node can easily be computed as follows. For I  1 to v // n vertices For J  1 to A[I].size() // neighbours of vertex I n  A[i].get(J) outdegree[I]++; indegree[n]++; Time complexity : O(V+E)

CS 307 Fundamentals of Computer Science 24 Problem  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 ?

CS 307 Fundamentals of Computer Science 25 Solution  Given the adjacency-Matrix representation of a graph the transpose can be computed as follows. For I  1 to v // v vertices For J  1 to v // v vertices Transpose[I][J] = A[J][I] Time complexity : O(V 2 )

CS 307 Fundamentals of Computer Science 26 Solution  Given the adjacency-List representation of a graph the transpose can be computed as follows. For I  1 to n // n vertices For J  1 to A[I].size() // neighbours of vertex I Transpose[I].add(A[J].get(I)) Time complexity : O(V+E)

CS 307 Fundamentals of Computer Science 27 Predecessor and Successor in a BST  Successor of node x is the node y such that key[y] is the smallest key greater than key[x].  The successor of the largest key is NIL.  Search consists of two cases. –If node x has a non-empty right subtree, then x’s successor is the minimum in the right subtree of x. –If node x has an empty right subtree, then: As long as we move to the left up the tree (move up through right children), we are visiting smaller keys. x’s successor y is the node that x is the predecessor of (x is the maximum in y’s left subtree). In other words, x’s successor y, is the lowest ancestor of x whose left child is also an ancestor of x.

CS 307 Fundamentals of Computer Science 28 Pseudo-code for Successor Tree-Successor(x)  if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y Tree-Successor(x)  if right[x]  NIL 2. then return Tree-Minimum(right[x]) 3. y  p[x] 4. while y  NIL and x = right[y] 5. do x  y 6. y  p[y] 7. return y Code for predecessor is symmetric. Running time: O(h)

CS 307 Fundamentals of Computer Science 29 Properties of Binary Trees Note that external nodes are also called leaf nodes Let # leaf nodes = E, # internal nodes = I, Height = H, # nodes = N, 1)E = I + 1 2)N = E + I 3)(# nodes at level x) <= 2 x 4)E <= 2 H 5)H >= log 2 I 6)A full binary tree has (2 H+1 – 1) nodes. 7)(H+1) ≤ E ≤ 2 H. 8)H≤ I ≤ 2 H -1. 9)2H+1 ≤ N ≤ 2 H ) log 2 (N+1)-1 ≤ H ≤ (N-1)/2 11) log 2 (E) ≤ H ≤ E-1 12) log 2 (I+1) ≤ H ≤ I