Title: Data Structures
1Chapter 7
2Variables vs. Arrays
- Let us suppose we want to store 15 numbers.
- One option is to create 15 variables in the
variable declaration. - The second option is to create an array.
3var num1 integer num2 integer num3
integer num4 integer num5 integer num6
integer num7 integer num8
integer num9 integer num10
integer num11 integer num12
integer num13 integer num14
integer num15 integer
4var num array 1..15 of integer
OR type numbers array 1..15 of
integer var num numbers
5Arrays
- use to store lists
- easy to work with
- pass entire array as one parameter
- represent other structures
- Queues
- Stacks
- Trees
- fixed size (most languages)
- upper limit specified at compile time
- too small
- too large
- waste memory
- primitive types
- store one item
- an array
- stores many items of the same type
- homogeneous storage
- Random access
- define type and array size
- some languages have fixed lower end (1 or 0)
C, Java - other languages allow the user to define the
lower and upper ends of the indices Pascal - to access an array element
- specify an index as in Ai or A5
6Array Access
7More Efficient Code
8Multi-dimensional Arrays
index
COLUMNS
num
1
2
3
- Its like a table
- Conceptual model
- Still stored in consecutive memory locations
- Save rows first or save information as columns
first?
num1,2
num2,3
num3,1
ROWS
num14,?
num15,?
9Implementation of Arrays
- A language must have a mechanism for determining
the storage location of an element in the array - consecutive memory locations
- Mapping function
- Conversion mechanism
- 1 dimensional array location offset
(i-1)unit_size - 2-d array location offset (i - 1) c (j
- 1) unit_size - row-major order (above)
- column-major order (FORTRAN)
10Pointers
- A memory cell that contains an address of a datum
(often referred to as NODES) - Still must be declared as a pointer
- (like a variable)
- Must create a new memory location for the pointer
to point to - Must get rid of memory after finishing
- garbage collection
- Memory leak
- NIL or NULL is used to fill a pointer when it
doesnt point to actual datum - Used for dynamic allocation
- Does use up more memory if considering the same
amount of data to be stored with variables
POINTER to an integer
123
INSTEAD of
123
Variable_name
11Pointer Hazards
- dangling pointers
- leaving a pointer not pointing to a current datum
(or NIL) - lost objects
- changing a pointer losing the initial object
- using pointer arithmetic
- in C, it was common to use integers as pointers
and use arithmetic to adjust pointers, but this
is not a good idea! - running out of heap memory (cant create new
memory locations)
12Lists
- ordered or unordered information
- can be accessed randomly
- Implementation
- Array
- Fixed size
- Shifting information up or down the array
- Pointers (Linked List)
- advantage is that it is dynamic, more memory
location can be allocated during run time or
delete after use of the memory location - Head pointer (Front of the List)
- (End of list) NIL or NULL
- Traversal (move through the list)
Datum
Pointer to another datum
13Conceptual List
- A user should not have to worry how the list is
actually implemented. - Just use the functions (or a collection of
procedures) that the list supports - Examples sorting, searching, accessing an item,
adding, deleting, printing, destroying - Abstract Data Type (ADT)
14Stacks
- Similar to a list but all accesses are not random
access - Access only at the top
- Add an item
- Remove an item
- LIFO (last in first out) structure
- Like a stack of trays in a cafeteria
- Operations
- create, destroy
- push -- add an item to the top
- pop -- remove an item from the top
- empty and full
- Uses/Applications
- used in mazes (backtracking)
- Operating System uses a run-time stack (programs)
- Procedures and functions are pushed onto the
stack when they are called
15Stacks
- Stack implementation
- Arrays
- Fixed size
- Index (Top moves and down the stack)
- Pointers
- Two pointers required (for traversal) to avoid
memory leaks - TOP
- TEMP
Top
Interior are inaccessible
Top
16Queues
- Similar to a list but all accesses are not random
access - Access at two ends
- Add an item at the end tail
- Remove an item at the front head
- FIFO (first in first out) structure
- Like a waiting line
- Operations
- create, destroy
- enqueue -- add an item to the queue
- dequeue -- remove an item from the queue
- empty and full
- Uses/Applications
- Used in simulations
- Operating System uses queues in certain
operations - a print queue
17Queues
- Queue implementation
- Arrays
- Fixed size
- Index (Both the Head index and Tail index move up
and down the queue) - Circular queues
- Pointers
- Three pointers required (for traversal) to
avoid memory leaks - HEAD
- TAIL
- TEMP
Head
Interior are inaccessible
Tail
Tail
Head
18Trees
- A tree is a structure in which items have more
than one successor - Access any node in the tree
- Depth-first traversals
- Breadth-first traversals
- Pre-Order traversals
- Post-Order traversals
- In-Order traaversals
- Operations
- create, destroy
- add item (node) to tree
- delete item (node) to tree
- Uses/Applications
- Ordered storage
- Logical organization of ideas
- Classification hierarchies organizational charts
19Trees
Root (Traverse with an index)
- Tree implementation
- Arrays
- Fixed size
- Index (use a single index to traverse the tree)
- left child at index 2i
- right child at index 2i1
- Pointers
- one pointers required (for traversal)
- TEMP
- difficult to traversal upwards
- recursion
- threaded trees
Root
Left
Right
Left
Right
20Trees
TEMP
21Tree Terminology
- A parent is a node that has children or siblings
nodes - The root node is the first node of the structure
- Leaf nodes are the bottom nodes terminal nodes
- A subtree is a smaller structure (tree) within
the tree - The depth of the tree is the number of levels (
of edges to the furthest leaf node) - A general tree is a tree where nodes can have any
number of children - A binary tree is a tree in which nodes have no
more than 2 children - A balanced tree is a tree where the nodes do not
all fall to one side of the tree
22Different Types of Trees
- Trees with different properties
- Binary Search Trees
- Items are arranged in such a way that all child
nodes to the left are smaller than the root
(subroot) and all child nodes to the right are
larger than the root (subroot). - AVL Height Balanced Trees
- A special binary tree where nodes are re-arranged
to keep a balanced tree. - Splay Trees
- A special version of the AVL tree
- Red/Black Trees (RBT)
- A balanced binary tree where nodes are colored
- Bayer Trees (B-Trees)
- A balanced general tree where the height is
important (less search space) database
applications
23Traversals
- Preorder traversal
- Visit root
- Visit subtree to the left
- Visit subtree to the right
- Inorder traversal
- Visit subtree to the left
- Visit root
- Visit subtree to the right
- Postorder traversal
- Visit subtree to the left
- Visit subtree to the right
- Visit root
- Example
- Preorder
- 8, 3, 1, 5, 10, 9, 13
- Inorder
- 1, 3, 5, 8, 9, 10, 13
- Postorder
- 1, 5, 3, 9, 13, 10, 8
24Another Binary TreeRepresentation
- Math expression in a binary tree representation
- ((xy)2)((x-4)/3)
- Traversals
- Preorder
- Prefix notation
- Polish notation
- Postorder
- Postfix notation
- Reverse Polish notation
- Prefix notation
- _________________
- Postfix notation
- _________________
25Graphs
- Mathematical objects where a node connects to
another node. (No hierarchy structured
required.a tree is a subset of graphs) Nodes
connected to each other. - a powerful tool for representing different types
of models - Example Networks
- Composed of the following
- vertices (vertex)
- Edges (connection between vertices)
- Directed Graphs
- Edges show direction movement of information
- Undirected Graphs
- Edges show no direction movement of information
26Graphs
- Graph implementation
- Arrays
- Sparse arrays
- Matrix or a table where 0 1s are used to
denote connections between vertices - Pointers
- Link lists
- A list of other vertex connected to the current
vertex
1
2
3
1
0
1
1
2
0
0
0
1
0
0
3
2
1
3
1
3
27Customized Data Types
- Primitive data types
- Integers, characters, real numbers, booleans,
pointers - User-defined types
- Structures or records
- Heterogeneous
- Different Types available
Example Struct Info char Name8
int Age ptr Next
28Abstract Data Types
- ADTs
- Predetermined storage system (data structure).
- Data types including User Defined Types.
- Operations to be performed on data structure.
- Encapsulation is an important idea.
- The user of the structure does not have to worry
about the details of the ADT. They just use the
structure (queue, list, stack). - Also they do not have access to other information
outside the way the data structure was intended
to be used. Hiding certain information
procedures from the user - Promote modularity and reuse of code.
29Object-Oriented Programming
- Strong encapsulation
- Objects (somewhat inherited idea from ADTs)
- Classes (template somewhat like structs)
- (instances of classes become objects)
- Own data types
- public, protected, private
- Own methods (functions that belong to that
object) - Public, protected, private