CSE 326: Data Structures Lecture #1 Lists, MultiLists & Trees

Slides:



Advertisements
Similar presentations
Binary Trees Chapter 6. Linked Lists Suck By now you realize that the title to this slide is true… By now you realize that the title to this slide is.
Advertisements

Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees. 2 Definition of a tree A tree is like a binary tree, except that a node may have any number of children Depending on the needs of the program,
Trees. Definition of a tree A tree is like a binary tree, except that a node may have any number of children –Depending on the needs of the program, the.
CSE 326: Data Structures Lecture #6 (end of Lists, then) Trees Alon Halevy Spring Quarter 2001.
Trees. 2 Definition of a tree A tree is a node with a value and zero or more children Depending on the needs of the program, the children may or may not.
CSE 326: Data Structures Lecture #5 Alon Halevy Spring Quarter 2001.
CSC 2300 Data Structures & Algorithms February 6, 2007 Chapter 4. Trees.
CS 146: Data Structures and Algorithms June 18 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Topic 14 The BinaryTree ADT Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Data Structures TREES.
CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues Steve Wolfman 2014W1 1.
CE 221 Data Structures and Algorithms Chapter 4: Trees (Binary) Text: Read Weiss, §4.1 – 4.2 1Izmir University of Economics.
Trees : Part 1 Section 4.1 (1) Theory and Terminology (2) Preorder, Postorder and Levelorder Traversals.
CSE 326: Data Structures Lecture #6 From Lists to Trees Henry Kautz Winter 2002.
M180: Data Structures & Algorithms in Java Trees & Binary Trees Arab Open University 1.
24 January Trees CSE 2011 Winter Trees Linear access time of linked lists is prohibitive  Does there exist any simple data structure for.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
CS Prelim Review – 10/15/09  First prelim is TOMORROW at 7:30 PM  Review session – Tonight 7:30 PM, Phillips 101  Things you should do:  Review every.
1 Trees : Part 1 Reading: Section 4.1 Theory and Terminology Preorder, Postorder and Levelorder Traversals.
18-1 Chapter 18 Binary Trees Data Structures and Design in Java © Rick Mercer.
Trees A non-linear implementation for collection classes.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
What is a Tree? Formally, we define a tree T as a set of nodes storing elements such that the nodes have a parent-child relationship, that satisfies the.
CSE 373 Data Structures Lecture 7
Topic 2: binary Trees COMP2003J: Data Structures and Algorithms 2
The Tree ADT.
Non Linear Data Structure
CS 201 Data Structures and Algorithms
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Trees Chapter 15.
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Data Structures and Design in Java © Rick Mercer
Fundamentals of Programming II Introduction to Trees
Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos
Week 6 - Wednesday CS221.
CSE 326: Data Structures Lecture #1 2 Degrees of List
Problems with Linked List (as we’ve seen so far…)
CMSC 341 Introduction to Trees.
CSE 373 Data Structures Lecture 7
Trees.
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
TREES General trees Binary trees Binary search trees AVL trees
Trees.
Trees.
Find in a linked list? first last 7  4  3  8 NULL
CSE 326: Data Structures Lecture #4 Mind Your Priority Queues
Binary Trees Based on slides by Alyssa Harding & Marty Stepp
Heaps and the Heapsort Heaps and priority queues
Week nine-ten: Trees Trees.
CMSC 202 Trees.
CSE 326: Data Structures Priority Queues & Binary Heaps
CE 221 Data Structures and Algorithms
Trees.
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Tree.
CS210- Lecture 9 June 20, 2005 Announcements
Trees.
Binary Trees.
Binary Trees, Binary Search Trees
Presentation transcript:

CSE 326: Data Structures Lecture #1 Lists, MultiLists & Trees Raise your hand if you can read. Raise your other hand if you can write. Stand up if you can think. WHOOP if you can talk. Point: this classroom is not sacred ground. I am not a fearsome taskmaster. Please take part in this class, the most horrible thing in the world for an instructor is unresponsive students; don’t be my nightmare! Bart Niswonger Summer Quarter 2001

Today’s Outline Things Bart Forgot (handouts) How Homework Works Project Guidelines & Forming Teams Lists (from Monday) Multi-lists Priority Queues

Homework Quiz on Friday Homework due Thursday lasts about 10 minutes in class what you get right counts toward homework what you get wrong becomes short answer for homework quiz returned on Monday Homework due Thursday turn in at the start of section Gilligan’s island rule This is how I’d like to do this, but I don’t know if it will work. I need feedback from all of you on that point. Quizzes will last about 10 minutes in class.

Project Guidelines & Teams Teams – collaboration encouraged – document! Document your code! Document everything!!!! Some people have had some trouble finding team members, and some people do not have much experience with UNIX and g++, so I want to work on forming some teams right now.

List ADT (review) ( A1 A2 … An-1 An ) List operations List properties Create/Destroy Length Find Insert/Remove Next/Previous List properties Ai precedes Ai+1 for 1  i < n Ai succeeds Ai-1 for 1 < i  n Size 0 list is defined to be the empty list () ( A1 A2 … An-1 An ) length = n Now, back to work! We’re going to talk about lists briefly and quickly get to an idea which I hope you haven’t seen. Lists are sets of values. The type of those values is arbitrary but fixed (can’t change from one to another in the same list). Each value is at a position, and those positions are totally ordered.

Applications Everything! Class list compilers: list of functions in a program, statements in a function graphics: list of triangles to be drawn to the screen operating systems: list of programs running music: compose crazy hard transcendental études other data structures: queues, stacks!

Iterators General method of examining collections List<Object> *list; Object x; ... ListItr<Object> *i = list->first(); while ( i->hasNext() ) { x = i->next(); } TANGENT There are different iterator models – the main ones being the Java model and the STL model – we will focus on the Java model because I find it cleaner, but they do the same thing

List Operations Iteration operations: Main operations: ListItr<Object> first() ListItr<Object> kth(int) ListItr<Object> last() Main operations: ListItr<Object> find(Object) void insert(Object, listItr<Object>) void remove(ListItr<Object>) bool isEmpty()

Application: Polynomial ADT Ai is the coefficient of the xn-i term: 3x2 + 2x + 5 ( 3 2 5 ) 8x + 7 ( 8 7 ) Here’s an application of the list abstract data type as a _data structure_ for another abstract data type. Is there a problem here? Why? x2 + 3 ( 1 0 3 ) Problem?

x2001 + 1 ( 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ) What is it about lists that makes this a problem here and not in stacks and queues? (Answer: kth(int)!) Is there a solution? Will we get anything but zeroes overwhelming this data structure?

Sparse List Data Structure (?): x2001 + 1 ( <1 2001> <1 0> ) Linked List vs. Array This slide is made possible in part by the sparse list data structure. Now, two questions: 1) Is a sparse list really a data structure or an abstract data type? (Answer: It depends but I lean toward data structure. YOUR ANSWER MUST HAVE JUSTIFICATION!) 2) Which list data structure should we use to implement it? Linked Lists or Arrays? 1 2001 1 2001

Addition of Two Polynomials Similar to merging two sorted lists 15+10x50+3x1200 p 15 10 50 3 1200 5+30x50+4x100 q 5 30 50 4 100 r 20 40 50 4 100 3 1200

Other Data Structures for Lists Doubly Linked List Circular List 7 11 3 2 Advantages/disadvantages (previous for doubly linked list) your book also describes header nodes. Are they just a hack? I’m not going to go into these, but: You should be able to (for a test) add and delete nodes in all these types of list; not to mention for your daily coding needs! c d e f

Multiple Linked Lists Many ADTS such as graphs, relations, sparse matrices, multivariate polynomials use multiple linked lists Several options array of lists lists of lists multi lists General principle throughout the course: use one ADT to implement a more complicated one.

Array of Linked Lists: Adjacency List for Graphs 1 3 2 5 4 G Graphs are a very important data type. You might think as you read about your project if there are any graphs there. Here, we’re implementing graphs with adjacency lists. The reason is that this is a sparse graph. We want to have every node in an array (so we can find the first edge quickly), but we just need the edges around. Array G of unordered linked lists Each list entry corresponds to an edge in the graph 1 5 2 2 4 3 5 3 1 4 4 5 4 5

Reachability by Marking Suppose we want to mark all the nodes in the graph which are reachable from a given node k. Let G[1..n] be the adjacency list rep. of the graph Let M[1..n] be the mark array, initially all falses. mark(int i){ M[i] = true; x = G[i] while (x != NULL) { if (M[x->node] == false) mark(G[x->node]) x = x->next } Here’s an algorithm that works on our adj list graph.

Thoughts on Reachability The marking algorithm visits each node and each edge at most once. Why? This marking algorithm uses Depth First Search. DFS uses a stack to track nodes. Where? Graph reachability is closely related to garbage collection the nodes are blocks of memory marking starts at all global and active local variables the marked blocks are reachable from a variable unmarked blocks are garbage Each node once because after once, it’s marked. Each edge once because we visit each node once and edges are stored under nodes.

MultiLists Discuss the multilist data structure, as described in the book why would you want this structure? Sparse data sets - multivariate expressions; enrollmentt? What about higher dimensions? possible, hurts the head

Trees Family Trees Organization Charts Classification trees what kind of flower is this? is this mushroom poisonous? File directory structure folders, subfolders in Windows directories, subdirectories in UNIX Non-recursive procedure call chains Anyone not seen a tree? I mean the kind that grows _down_ from the root. The computer science tree. Here are some applications. What do these have in common? They have a root. Each entry is either a leaf or it has a number of children. Each child is also a tree. Trees are defined recursively! A tree is a leaf or an internal node with one or more children. Each child is a tree.

Tree Terminology A root: leaf: child: parent: sibling: ancestor: descendent: subtree: B C D E F G Let’s review the words: root: A leaf: DEFJKLMNI child:A - C or H - K leaves have no children parent: C - A or L - H the root has no parent sibling: D - E or F or J - K,L,M, or N grandparent: G to A grandchild: C to H or I ancestor: the node itself or any ancestor’s parent descendent: the node itself or any child’s descendent subtree: a node and all its descendents H I J K L M N

More Tree Terminology A depth: height: degree: branching factor: preorder traversal: postorder traversal: B C D E F G Depth: the number of edges along the path from the root to the node height: the number of edges along the longest path from the node to a leaf degree: the number of children of the node branching factor: the maximum degree of any node (or sometimes the average) preorder traversal: running through all the nodes in the tree, starting with the parent, then all the children postorder traversal: run through all the nodes starting with the children and then the parents H I J K L M N

One More Tree Terminology Slide binary: n-ary: complete: A B C Binary: each node has at most two children n-ary: each child has at most n children complete: Each row of the tree is filled in left to right before the next one is started HOW DEEP CAN A COMPLETE TREE BE? D(N)  D(N/2) + 1 D E F G H I J

Tree Calculations Find the longest undirected path in a tree Might be: OK, let’s get a bit of practice working with trees. Almost anything we do on a tree will be most easily thought of as recursive (even if we implement it iteratively) Let’s try to find the longest undirected path through the tree. That is, the path goes from node to node, never repeating a node, and it can either go up a link or down it. Thinking recursively, what might the longest path in this example tree be? EITHER: the longest path in one of the subtrees OR: the longest path that goes through t To implement that, we could keep returning the height of the tree and the longest path in the tree. The height overall is the max height + 1. The longest path is max(longest subtree path, 2max(heights)+1

Tree Calculations Example B C D E F G OK, what’s the longest path in this tree? H I J K L M N

To Do Subscribe to Mailing List Form teams Start Project I Read chapter 6 in the book Think about whether you like this homework format

Coming Up Templates Tutorial tomorrow More Priority Queue Operations during section – 10:50 in GUG 410 More Priority Queue Operations Mergeable Priority Queues First Quiz (Friday June 22nd) First project due (Wednesday June 27th)