Data Structures — Lists and Trees CS-2301, B-Term 20091 Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.

Slides:



Advertisements
Similar presentations
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Advertisements

Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 abstract containers hierarchical (1 to many) graph (many to many) first ith last sequence/linear (1 to 1) set.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures, Lists, and Trees CS-2301 B-term Data Structures — Lists and Trees CS-2301, System Programming for Non-majors (Slides include materials.
Binary Trees Chapter 6.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
COMP261 Lecture 23 B Trees.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Introduction to Linked Lists
12 C Data Structures.
Data Structures Interview / VIVA Questions and Answers
Chapter 20: Binary 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.
Introduction to Linked Lists
Chapter 21: Binary Trees.
Programming Assignment #4 Binary Trees in C++
Linked Lists.
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Introduction to C++ Linear Linked Lists
Presentation transcript:

Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Data Structures — Lists and Trees CS-2301, B-Term Reading Assignment Chapter 6 of Kernighan and Ritchie AGAIN!

Data Structures — Lists and Trees CS-2301, B-Term Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem –Where the data should flow so it is available when you need it –How your data can accommodate change and evolution of … … your own program … the requirements of your application

Data Structures — Lists and Trees CS-2301, B-Term Support from Programming Languages E.g., Java knows about all kinds of Lists, trees, arrays, collections You tell it what you want and it does the rest E.g., Scheme is entirely built on lists Anything a list can do is easy! Anything a list cannot do is hard! E.g., Matlab is about matrices and vectors Extensive support for linear and non-linear algebras

Data Structures — Lists and Trees CS-2301, B-Term In the case of C You are on your own! Only built-in tools –Arrays –structs and unions –Functions Everything must be done “long-hand”

Data Structures — Lists and Trees CS-2301, B-Term Theoretically Every computational problem can be solved with loops, arrays, non-recursive functions, and an unlimited amount of memory. I.e., in Fortran! In reality, most real-life problems are much, much too hard to solve that way

Data Structures — Lists and Trees CS-2301, B-Term Common Data Structures for Real-Life Problems Linked lists One-way Doubly-linked Circular Trees Binary Multiple branches Hash Tables Combine arrays and linked list Especially for searching for objects by value

Data Structures — Lists and Trees CS-2301, B-Term Definitions Linked List A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Singly- or doubly-linked Stack, queue, circular list Tree A data structure in which each element is dynamically allocated and in which each element has more than one potential successor Defines a partial order Note: elements are usually the same type (but not always).

Data Structures — Lists and Trees CS-2301, B-Term Linked List struct listItem { type payload; struct listItem *next; }; payload next payload next payload next payload next Note: payload may be multiple members.

Data Structures — Lists and Trees CS-2301, B-Term Linked List (continued) Items of list are usually same type Generally obtained from malloc() Each item points to next item Last item points to null Need “ head ” to point to first item! “Payload” of item may be almost anything A single member or multiple members Any type of object whose size is known at compile time Including struct, union, char * or other pointers Also arrays of fixed size at compile time (see p. 214)

Data Structures — Lists and Trees CS-2301, B-Term Usage of Linked Lists Not massive amounts of data Linear search is okay Sorting not necessary or sometimes not possible Need to add and delete data “on the fly” Even from middle of list Items often need to be added to or deleted from the “ends”

Data Structures — Lists and Trees CS-2301, B-Term Linked List (continued) struct listItem { type payload; struct listItem *next; }; struct listItem *head; payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term Adding an Item to a List struct listItem *p, *q; Add an item pointed to by q after item pointed to by p –Neither p nor q is NULL payload next payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term Adding an Item to a List (continued) listItem *addAfter(listItem *p, listItem *q){ if (p && q) { q -> next = p -> next; p -> next = q; } return p; } payload next payload next payload next payload next payload next Note test for non-null p and q

Data Structures — Lists and Trees CS-2301, B-Term What about Adding an Item before another Item? struct listItem *p; Add an item before item pointed to by p ( p != NULL ) payload next payload next payload next payload next payload next

Data Structures — Lists and Trees CS-2301, B-Term What about Adding an Item before another Item? Answer:– –Need to search list from beginning to find previous item –Add new item after previous item

Data Structures — Lists and Trees CS-2301, B-Term Doubly-Linked List struct listItem { type payload; listItem *prev; listItem *next; }; struct listItem *head, *tail; prevnext payload prevnext payload prevnext payload prevnext payload In-class exercise:– how to add a new item q after a list item p

Data Structures — Lists and Trees CS-2301, B-Term Other Kinds of List Structures Queue — FIFO (First In, First Out) Items added at end Items removed from beginning Stack — LIFO (Last In, First Out) Items added at beginning, removed from beginning Circular list Last item points to first item Head may point to first or last item Items added to end, removed from beginning

Data Structures — Lists and Trees CS-2301, B-Term Questions?

Data Structures — Lists and Trees CS-2301, B-Term Definitions Linked List A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Singly- or doubly-linked Stack, queue, circular list Tree A data structure in which each element is dynamically allocated and in which each element has more than one potential successor Defines a partial order

Data Structures — Lists and Trees CS-2301, B-Term Binary Tree A linked list but with two links per item struct treeItem { type payload; treeItem *left; treeItem *right; }; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Data Structures — Lists and Trees CS-2301, B-Term Binary Tree (continued) Binary tree needs a root struct treeItem { type payload; treeItem *left; treeItem *right; }; struct treeItem *root; Binary trees often drawn with root at top! Unlike ordinary trees in the forest More like the root systems of a tree

Data Structures — Lists and Trees CS-2301, B-Term Binary Tree struct treeItem { type payload; treeItem *left; treeItem *right; }; struct treeItem *root; leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Data Structures — Lists and Trees CS-2301, B-Term Purpose of a Tree (Potentially) a very large data structure Capable of storing very many items Need to find items by value I.e., need to search through the data structure to see if it contains an item with the value we want Need to add new items If value is not already in the tree, add a new item … …so that it can be easily found in future Why not use a linked list?

Data Structures — Lists and Trees CS-2301, B-Term Searching and Adding to a Binary Tree Look recursively down sequence of branches until either –Desired node is found; or –Null branch is encountered Replace with ptr to new item Decide which branch to follow based on payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Data Structures — Lists and Trees CS-2301, B-Term Example — Searching a Tree typedef struct _treeItem { char *word;// part of payload int count;// part of payload _treeItem *left, *right; } treeItem; treeItem *findItem(treeItem *p, char *w) { if (p == NULL) return NULL;// item not found int c = strcmp(w, p->word); if (c == 0) return p; else if (c left, w); else return findItem(p->right, w); }

Data Structures — Lists and Trees CS-2301, B-Term Why do this? Example — Adding an Item treeItem *addItem(treeItem *p, char *w) { if (p == NULL){ p = malloc(sizeof(treeItem)); char *c = malloc(strlen(w)+1); p->word = strcpy(c, w); p->count = 1; p->left = p->right = NULL; return p; }; int c = strcmp(w, p->word); if (c == 0) p->count++; else if (c left = addItem(p->left, w); else p->right = addItem(p->right, w); return p; }

Data Structures — Lists and Trees CS-2301, B-Term Binary Tree Question:– how many calls to addItem for a tree with 10 6 nodes? –Assume balanced –I.e., approx same number of nodes on each subtree leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload leftright payload

Data Structures — Lists and Trees CS-2301, B-Term Observation Problems like this occur in real life all the time Need to maintain a lot of data Usually random Need to search through it quickly Need to add (or delete) items dynamically Need to sort “on the fly” I.e., as you are adding and/or deleting items

Data Structures — Lists and Trees CS-2301, B-Term Questions?

Data Structures — Lists and Trees CS-2301, B-Term Answer Approximately 20 calls to addItem Note:– –2 10 = 1024  10 3 –Therefore 10 6  2 20 –Therefore it takes approximately 20 two-way branches to cover 10 6 items! How many comparisons would it take to search a linked list of 10 6 items?

Data Structures — Lists and Trees CS-2301, B-Term Binary Trees (continued) Binary tree does not need to be “balanced” i.e., with approximate same # of nodes hanging from right or left However, it often helps with performance Multiply-branched trees Like binary trees, but with more than two links per node

Data Structures — Lists and Trees CS-2301, B-Term Binary Trees (continued) Binary tree does not need to be “balanced” i.e., with approximate same # of nodes hanging from right or left However, it helps with performance Time to reach a leaf node is O(log 2 n), where n is number of nodes in tree Multiply-branched trees Like binary trees, but with more than two links per node “Big-O” notation:– means “order of”

Data Structures — Lists and Trees CS-2301, B-Term Binary Tree Example – PA5 #5 Payload:– char *word — the word at that node int count — number of occurrences Possibly other data When we are pointing to any node in the tree and have a word w, either:– w is the same word as at that node, so just increase its count, w is alphabetically before the word at that node, so look for it in the left subtree, w is alphabetically after the word at that node, so look for it in the right subtree, or The node is empty (i.e., null), so create one for that word.