Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #1 Lists, MultiLists & Trees"— Presentation transcript:

1 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

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

3 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.

4 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.

5 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.

6 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!

7 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

8 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()

9 Application: Polynomial ADT
Ai is the coefficient of the xn-i term: 3x2 + 2x + 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 ( ) Problem?

10 x ( ) 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?

11 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

12 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

13 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

14 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.

15 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

16 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.

17 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.

18 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

19 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.

20 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

21 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

22 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

23 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

24 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

25 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

26 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)


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

Similar presentations


Ads by Google