Data Structures and Analysis (COMP 410)

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Interval Heaps Complete binary tree. Each node (except possibly last one) has 2 elements. Last node has 1 or 2 elements. Let a and b be the elements in.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Dynamic Memory Allocation I Topics Basic representation and alignment (mainly for static memory allocation, main concepts carry over to dynamic memory.
Pointers & Dynamic Memory Allocation Mugurel Ionu Andreica Spring 2012.
Review for Final Exam Dilshad M. NYU. In this review Arrays Pointers Structures Java - some basic information.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Management Memory Areas and their use Memory Manager Tasks:
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Data Representation and Alignment Topics Simple static allocation and alignment of basic types and data structures Policies Mechanisms.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C programming or Fun with pointers Tutorial #2 CPSC 261.
David Stotts Computer Science Department UNC Chapel Hill.
David Stotts Computer Science Department UNC Chapel Hill.
Memory organization and usage A computer’s memory is, at its lowest level, composed of binary digits (bits). The memory is organized into bytes, which.
Trees and beyond Tutorial #3 CPSC 261. Trees are just an example The next two weeks in labs we are playing with trees – Trees are interesting – Trees.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
David Stotts Computer Science Department UNC Chapel Hill.
Topics memory alignment and structures typedef for struct names bitwise & for viewing bits malloc and free (dynamic storage in C) new and delete (dynamic.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
IT11004: Data Representation and Organization Floating Point Representation.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
First Foray into Programming (the hard way). A reminder from last lesson: A machine code instruction has two parts:  Op-code  Operand An instruction.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
STACKS & QUEUES for CLASS XII ( C++).
EGR 2261 Unit 11 Pointers and Dynamic Variables
Stack and Heap Memory Stack resident variables include:
Memory Management Memory Areas and their use Memory Manager Tasks:
Data Structures and Analysis (COMP 410)
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Chapter 6: Data Types Lectures # 10.
Data Structure and Algorithms
Memory Management Memory Areas and their use Memory Manager Tasks:
Data Structures and Analysis (COMP 410)
C++ Interlude 2 Pointers, Polymorphism, and Memory Allocation
Data Structures and Analysis (COMP 410)
Chapter 8: Data Abstractions
Computer science C programming language Lesson 5
Heterogeneous Data Structures & Alignment
Data Structures and Analysis (COMP 410)
Arrays and Linked Lists
Data Structures and Analysis (COMP 410)
Data.
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Topic 3-b Run-Time Environment
By Hector M Lugo-Cordero September 17, 2008
KENDRIYA VIDYALAYA SANGATHAN (AGRA REGION)
Data Structures and Analysis (COMP 410)
Chapter 17: Linked Lists.
Data Structures and Analysis (COMP 410)
Data Structures and Analysis (COMP 410)
Dynamic Memory A whole heap of fun….
By Yogesh Neopaney Assistant Professor Department of Computer Science
Amortized Analysis and Heaps Intro
CS2013 Lecture 7 John Hurley Cal State LA.
Memory Management Memory Areas and their use Memory Manager Tasks:
IT11004: Data Representation and Organization
LINKED LIST Dr. T. Kokilavani Assistant Professor
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
CS148 Introduction to Programming II
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Data Structures and Analysis (COMP 410) David Stotts Computer Science Department UNC Chapel Hill

Representing Trees as Arrays

Array Rep for any BT Not heap structure (complete binary tree) 4 18 11 9 3 7 16 21 12 Not heap structure (complete binary tree) just a random binary tree 7 21 4 18 3 11 12 16 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Now get bigger… Add one node, double space needed in array 4 18 11 9 3 7 16 21 12 Add one node, double space needed in array 5 7 21 4 18 3 11 12 16 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 5 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

4 18 11 9 3 7 16 21 12 5 And bigger… 7 21 4 18 3 11 12 16 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 5 8 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 8 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

Array space used Some languages (without objects) would use huge space Array element is a static collection of data, each element might need 1000’s bytes array[10000] of struct { int, real, char[48] }

array int real chars int real chars 1 . . .

Array space used In Java (and languages with objects, pointers) can improve this Array element is an object, then each space in the array is a 8-byte address Each object is a collection of data, might be large, but we only need a object for each occupied array location class Element { int, real, char[48] } array[10000] of Element array[0] = new Element { 3, 4.5, “now is the time”} array[4] = new Element { 1, 6.3, “for all good code”}

. . . 3 4.5 “now is the time” 2 6.3 “for all good code” array 1 2 3 4 3 4.5 “now is the time” 1 2 3 4 5 6 7 2 6.3 “for all good code” . . .

Static layout, big space for each array element, used or not dynamic layout, each array element 64-bits, then big space for only elements used

Other problems… Add a node 8 3 and 16 have now changed levels When we add/move new nodes in a tree in the middle layers, we force rearrangement of values stored in the array Ex: 4 18 11 9 3 7 16 21 12 8 4 18 11 9 3 7 16 21 12 Add a node 8 between the 21 and 3 3 and 16 have now changed levels

Inner insert encoding the original tree 4 18 11 9 3 7 16 21 12 encoding the original tree 7 21 4 18 3 11 12 16 9 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Inner insert Add a node 8 between the 21 and 3 4 18 11 9 3 7 16 21 12 8 Add a node 8 between the 21 and 3 7 21 4 18 11 12 9 3 16 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Summary Array representation can be fast if the tree is built and then not changed (much) Changes in a tree require a lot of array element movement Array rep can be space inefficient if tree is not fairly full Array rep can be very space inefficient if array elements are static memory blocks rather than dynamically allocated objects

Beyond this is just templates END Beyond this is just templates