Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Dynamic memory allocation
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Dynamic memory allocation in C and C++ 程式設計 潘仁義 CCU COMM.
Binary Tree A data structure whose nodes contain two pointer fields. One or both pointers can have the value NULL. Each node in a binary tree can have.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
User-defined Structure Types Structures: collection of data of different types. We can define a structure type student_t : typedef struct { char name[10];
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
Chapter 14 Dynamic Data Structures Instructor: Alkar & Demirer.
CS Data Structures Chapter 4 Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Data Structures Using C++ 2E
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Chapter 19: Binary Trees. Objectives In this chapter, you will: – Learn about binary trees – Explore various binary tree traversal algorithms – Organize.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Pointers and Dynamic Data Structures Problem Solving, Abstraction,
Introduction to Data Structures Systems Programming.
Trees EENG212 Algorithms and Data Structures. Trees Outline  Introduction to Trees  Binary Trees: Basic Definitions  Traversing Binary Trees  Node.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Introduction to Data Structures Systems Programming Concepts.
Chapter 8 Arrays Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Copyright © 2002 Pearson Education, Inc. Slide 1.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
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.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
© 2012 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Problem Solving & Program Design in C Seventh Edition By Jeri R. Hanly.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
LINKED LISTS.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C++ Programming:. Program Design Including
Chapter 12 – Data Structures
Top 50 Data Structures Interview Questions
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
12 C Data Structures.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Chapter 14: Dynamic Data Structures
Dynamic memory allocation in C and C++
Review & Lab assignments
Pointers & Dynamic Data Structures
Dynamic memory allocation in C and C++
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Dynamic memory allocation in C and C++
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.2 Dynamic Data Structure Dynamic data structureDynamic data structure is a structure that can expand and contract as a program executes. pointersThe creation and manipulation of dynamic data structures requires use of pointers. pointer –A pointer is a variable which stores the memory address of a data value.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.3 Comparison of Pointer and Nonpointer Variables The actual data value of a pointer variable is accessed indirectly. The actual data value of a nonpointer variable can be accessed directly. Pointer variable Nonpointer variable

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.4 Pointer Review A call to a function with pointer parameters may need to use the & operator. –e.g., if we have an int variable value1 and f1(int *value), f1(&value1) is a legal call. A pointer can be used to represent an array. –e.g., char n[] is equal to char *n. A pointer can also represent a structure. –e.g., File * is a pointer to a File structure.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.5 Memory Allocation (1/3) C provides a memory allocation function called malloc, which resides in the stdlib library. –This function requires an argument which indicates the amount of memory space needed. –The returned data type is (void *) and should be always cast to the specific type. E.g., Declaration: int *nump; char *letp; planet_t *planetp;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.6 Memory Allocation (2/3) Allocation: nump = (int *) malloc (sizeof (int)); letp = (char *) malloc (sizeof (char)); planetp = (planet_t *) malloc (sizeof (planet_t)); Assignment: *nump = 307; *letp = ‘Q’; *planetp = blank_planet;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7 Memory Allocation (3/3) Memory space after allocation Memory space after assignment Pointers

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.8 Heap and Stack HeapHeap is the region of memory where function malloc dynamically allocates space for variables. StackStack is the region of memory where function data areas are allocated and reclaimed.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9 Dynamic Array Allocation C provides a function calloc which creates an array of elements of any type and initializes the array elements to zero. –Function calloc takes two arguments: the number of array elements and the size of one element. E.g., int *array_of_nums; array_of_nums = (int *) calloc(10, sizeof(int));

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10 Free Memory The allocated memory space can be released by the function free. –E.g., free(letp) returns the allocated memory space for the pointer variable letp. Once the memory space is released, we can not access the space again. Otherwise, it is considered as an illegal memory access.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.11 Multiple Pointers to a Cell double *xp, xcopyp; xp=(double *)malloc(sizeof(double)); *xp=49.5; xcopyp=xp; Be careful when releasing memory since the other pointer may still access the memory space.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.12 Linked List linked listA linked list is a sequence of nodes in which each node is linked to the node following it. In C, each node can be represented by a struct: typedef struct node_s{ char current[3]; int volts; struct node_s *linkp; }node_t;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.13 Creating Basic Nodes (1/2) node_t *n1_p, *n2_p, *n3_p; n1_p = (node_t *) malloc (sizeof(node_t)); strcpy(n1_p->current, “AC”); n1_p->volts = 115; n2_p = (node_t *) malloc (sizeof(node_t)); strcpy(n2_p->current, “DC”); n2_p->volts = 12; n3_p = n2_p;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.14 Creating Basic Nodes (2/2)

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.15 Linking Two Nodes n1_p->linkp = n2_p; “ n2_p->volts ” is equal to “ n1_p-> linkp->volts ”

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.16 Three-Node Linked List n2_p->linkp = (node_t *)malloc(sizeof(node_t)); strcpy(n2_p->linkp->current, “AC”); n2_p->linkp->volts = 220;

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.17 Three-Element Linked List The end of a linked list is usually terminated with a null pointer. –n2_p->linkp->linkp = NULL; –The following graph shows a complete linked list whose length is three. list head –The pointer variable n1_p points to the list head.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.18 Linked List After an Insertion We can easily insert or delete a node to or from a linked list. –The following graph shows an insertion of a new node containing “DC 9” between the second and last nodes. –Redirects the linkp of the new node to the last node. –Redirects the linkp of the second node to the new node.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.19 Traversing a Linked List Recursively or Iteratively We can print each element in a linked list recursively or iteratively. Recursive solutionIterative solution

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.20 Binary Tree We can extend the concept of linked list to binary trees which contains two pointer fields. –Leaf node: a node with no sucessors –Root node: the first node in a binary tree. –Left/right subtree: the subtree pointed by the left/right pointer.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.21 Binary Search Tree binary search treeA binary search tree is either empty or has the property that the item in its root has –a larger key than each item in the left subtree, and –a smaller key than each item in its right subtree.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.22 Searching a Binary Search Tree If the tree is empty –The target key is not in the tree else if the target key is the root’s key –The target key is found else if the target key is smaller than root’s key –Search the left subtree else –Search the right subtree

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.23 Searching a Binary Search Tree Assume the target key is 42.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.24 Building a Binary Search Tree If the tree is empty –Insert the new key in the root node else if the root’s key matches the new key –Skip insertion else if the new key is smaller than root’s key –Insert the new key in the left subtree else –Insert the new key in the right subtree

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.25 Building a Binary Search Tree Assume 40, 20, 10, 50, 65, 45, 30 are inserted in order.

Copyright ©2004 Pearson Addison-Wesley. All rights reserved.26 Building a Binary Search Tree The above algorithm implies a recursive implementation. Recursive step