UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

2012: J Paul GibsonT&MSP: Mathematical FoundationsMAT7003/L2-GraphsAndTrees.1 MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson,
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.
Binary Search Trees Azhar Maqsood School of Electrical Engineering and Computer Sciences (SEECS-NUST)
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
CMPS 2433 Discrete Structures Chapter 5 - Trees R. HALVERSON – MIDWESTERN STATE UNIVERSITY.
1 Part 1 The Prolog Language Chapter 9 Operations on Data Structure.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Blind State-Space Search Notes for Ch.11 of Bratko For CSCE 580 Sp03 Marco.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Trees and Red-Black Trees Gordon College Prof. Brinton.
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,
CS 307 Fundamentals of Computer Science 1 Data Structures Review Session 2 Ramakrishna, PhD student. Grading Assistant for this course.
Fundamentals of Python: From First Programs Through Data Structures
1 Section 9.2 Tree Applications. 2 Binary Search Trees Goal is implementation of an efficient searching algorithm Binary Search Tree: –binary tree in.
Important Problem Types and Fundamental Data Structures
Version TCSS 342, Winter 2006 Lecture Notes Trees Binary Trees Binary Search Trees.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Data Structures - CSCI 102 Binary Tree In binary trees, each Node can point to two other Nodes and looks something like this: template class BTNode { public:
Advanced Algorithms Analysis and Design Lecture 8 (Continue Lecture 7…..) Elementry Data Structures By Engr Huma Ayub Vine.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Tree.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
S EARCHING AND T REES COMP1927 Computing 15s1 Sedgewick Chapters 5, 12.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
1 Section 1.4 Graphs and Trees A graph is set of objects called vertices or nodes where some pairs of objects may be connected by edges. (A directed graph.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Computer Science: A Structured Programming Approach Using C Trees Trees are used extensively in computer science to represent algebraic formulas;
Binary Search Tree Traversal Methods. How are they different from Binary Trees?  In computer science, a binary tree is a tree data structure in which.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Agenda Review: –Planar Graphs Lecture Content:  Concepts of Trees  Spanning Trees  Binary Trees Exercise.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
Chapter 12 Abstract Data Type. Understand the concept of an abstract data type (ADT). Understand the concept of a linear list as well as its operations.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
ADT Binary Search Tree Ellen Walker CPSC 201 Data Structures Hiram College.
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.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables I.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
1 Trees. 2 Trees Trees. Binary Trees Tree Traversal.
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
Trees Chapter 15.
Chapter 5 : Trees.
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
Binary search tree. Removing a node
Binary Search Tree (BST)
Source Code for Data Structures and Algorithm Analysis in C (Second Edition) – by Weiss
(edited by Nadia Al-Ghreimil)
CSCE350 Algorithms and Data Structure
Binary Search Trees Chapter 9 2/22/2019 B.Ramamurthy.
(edited by Nadia Al-Ghreimil)
Binary Search Trees Chapter 9 2/24/2019 B.Ramamurthy.
CSC 143 Binary Search Trees.
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Trees.
Chapter 12&13: Binary Search Trees (BSTs)
Presentation transcript:

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco Valtorta

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Quicksort Delete some element X from L and split the rest of the list into two lists, Small and Big, as follows: Big contains all elements of L that are bigger than X; Small contains all other elements Sort Small obtaining SortedSmall Sort Big obtaining SortedBig The whole sorted list is the concatenation of SortedSmall, [X], and SortedBig fig9_2.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Quicksort with Difference Lists quicksort1 in fig9_3.pl (with split in fig9_2.pl) concat( A1-Z1,Z1-Z2,A1-Z2) Example: unify concat( A1-Z1,Z1-Z2,A1-Z2) concat( [a,b,c|T1]-T1,[d,e|T2]-T2,L) –Obtain: A1 = [a,b,c|T1] T1 = Z1 = [d,e|T2] Z2 = T2 L = [a,b,c,d,e|T2]-T2

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Mergesort ch9_1.pl mergesort is faster than quicksort on large arrays, on average, at least in some Prolog implementations mergesort has much better worst-case behavior than quicksort

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Binary Trees Binary trees Binary search trees –All nodes in the left subtree are less than the root –All nodes in the right subtree are greater than the root –Good implementation of dictionaries (if balanced)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Binary Tree Representation Use an atom to represent the empty tree –nil Use a special functor to indicate a non-empty tree –t( L,X,R) –Where X is the root, L is the left subtree and R is the right subtree –E.g., t( t( nil,b,nil),a,t( t( nil,d,nil),c,nil)) a b c d

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Binary Search Trees Search in BST: in( X,S): fig9_7.pl Inserting a leaf in the BST: addleaf( Tree,X,NewTree): fig9_10.pl Deleting from the BST del( Tree,X,NewTree): fig9_13.pl Inserting anywhere in the BST add( Tree,X,NewTree): fig9_15.pl Reversible: can be used to delete! Use the program of fig9_17.pl to display

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Adding a Leaf addleaf( D, X, D1)—adding X as a leaf to D gives D1 Adding X to the empty tree is t( nil,X,nil) If X is the root of D then D1 = D (no duplicates) If the root of D is greater than X than add X into the left subtree of D; if the root of D is less than X, then insert into the right subtree of D fig9_10.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Deleting from a BST Reversing addleaf does not work addleaf only adds at the leaf level addleaf does not correctly remove an internal node We take the minimum item in the right subtree of the node to be deleted and replace the deleted node with it (cf. Fig 9.12) delmin( Tree,Y,Tree1) if Y is the minimal (i.e., leftmost) node in Tree and Tree1 is Tree with Y deleted del( Tree,X,NewTree): fig9_13.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering General add and delete To add X to a binary dictionary D, either –(1) add X at the root of D, or –(2) if the root of D is greater than X then insert X into the left subtree, else insert X into the right subtree of D The difficult part is (1) –addroot( D,X,D1)--- add X to D obtaining D1 –See Fig. 9.14

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Figure 9.14 L1 and L2 must be BSTs The union of L1 and L2 is L All nodes in L1 are less than X, and all nodes in L2 are greater than X addroot imposes these constraints: If X were added as the root into L, then the subtrees of the resulting tree would be L1 and L2 Fig9_15.pl

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Representing Graphs By edges, e.g. connected( a,b). connected( b,c). arc( s,t,3). arc( t,v,1). arc( u,t,2). –(weighted digraph) –Analogous to representation for FSAs As a pair of sets, each represented by a list: –G1 = graph([a,b,c,d],[e(a,b),e(b,d),e(b,c),e(c,d)]). –G2 = digraph([s,t,u,v], [a(s,t,3),a(t,v,1),a(t,u,5),a(u,t,2),a(v,u,2)]).

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Representing Graphs II Using adjacency lists –G1 = [a->[b],b->[a,c,d],c->[b,d],d->[b,c]] –G2 = [s->[t/3],t->[u/5,v/1],u->[t/2],v->[u/2]]

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Finding a (Simple) Path path( A,Z,G,P) if –P is a (simple) path from A to Z in G –A path is defined here as a list of nodes (not a list of edges) If A = Z then P [A], otherwise Find a path P1 from some node Y to Z, and find a path from A to Y avoiding the nodes in P1

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Simple Paths in Graphs Program to find a simple path: fig9_20.pl –Program includes code for Hamiltonian paths Extra parentheses are needed for not (in SWI- Prolog, as usual) Spanning trees (fig9_22.pl, fig9_23.pl)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Paths in Weighted Graphs Extra arguments for costs: path( A,Z,G,P,C) path1( A,P1,C1,G,P,C) Program 9_21 Extra parentheses needed for not (as usual) Test1: any path Test2: shortest path (very inefficient: will see better ways soon) Test3: longest path (very inefficient)

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Spanning Trees A spanning tree of a graph G = (V,E) is a graph T = (V,E’) such that –T is connected –There is no cycle in T –E’ is a subset of E

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Spanning Trees: a Procedural Solution stree(G,T) if T is a spanning tree of G Spread( Tree1,Tree,Graph) if –Tree is a spanning tree of Graph obtaining by adding zero or mode edges to Tree1 Program fig9_22.pl Program uses “edge list” representation of graphs –Program can easily be modified to implement the Jarník-Prim-Dijkstra algorithm for minimum spanning trees

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Spanning Trees: a Declarative Solution G and T are represented as lists of edges T is a spanning tree of G if T is a subset of G T is a tree T covers G, i.e. each node of G is also in T A set of edges T is a tree if T is connected T has no cycle Program fig9_23.pl Is inefficient Modification can compute minimum spanning trees