26
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台台台台 台台台 )

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

Embed Size (px)

Citation preview

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

Chapter 14Dynamic Data Structures

Instructor:Kun-Mao Chao(台大資工 趙坤茂 )

Page 2: 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.

• The creation and manipulation of dynamic data structures requires use of pointerspointers.– A pointerpointer is a variable which stores the memory

address of a data value.

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

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

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

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.

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

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;

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

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;

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7

Memory Allocation (3/3)

Memory space after allocation

Memory space after assignment

PointersPointers

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

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.

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

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

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

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.

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

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.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 12

Linked List

• A linked listlinked 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;

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

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;

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 14

Creating Basic Nodes (2/2)

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

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”

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

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;

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

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.– The pointer variable n1_p points to the list headlist head.

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

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.

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

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 solution Iterative solution

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

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.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 21

Binary Search Tree

• A binary search treebinary 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.

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 23

Searching a Binary Search Tree

Assume the target key is 42.

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 25

Building a Binary Search TreeAssume 40, 20, 10, 50, 65, 45, 30 are inserted in order.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 26

Building a Binary Search TreeThe above algorithm implies a recursive implementation.

Recursive step

Recursive step