Overview of Data Structures, Lecture notes of Data Structures and Algorithms

Most Important Concrete Data Structures. ➢Arrays. ➢Records. ➢Linked Lists. ➢Binary Trees. Unit 3- Concrete Data Types. 2. Overview of Data Structures.

Typology: Lecture notes

2022/2023

Uploaded on 05/11/2023

jugnu900
jugnu900 🇺🇸

4.4

(7)

235 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT 3
Concrete Data Types
Classification of Data Structures
Concrete vs. Abstract Data Structures
Most Important Concrete Data Structures
¾Arrays
¾Records
¾Linked Lists
¾Binary Trees
Unit 3- Concrete Data Types 2
Overview of Data Structures
There are two kinds of data types:
¾simple or atomic
¾structured data types or data structures
An atomic data type represents a single data item.
A data structure , on the other hand, has
¾a number of components
¾a structure
¾a set of operations
Next slide shows a classification of the most important
data structures (according to some specific properties)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Overview of Data Structures and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

UNIT 3

Concrete Data Types

„ Classification of Data Structures „ Concrete vs. Abstract Data Structures „ Most Important Concrete Data Structures ¾Arrays ¾Records ¾Linked Lists ¾Binary Trees

Overview of Data Structures

„ There are two kinds of data types:

¾ simple or atomic ¾ structured data types or data structures

„ An atomic data type represents a single data item.

„ A data structure , on the other hand, has

¾ a number of components ¾ a structure ¾ a set of operations

„ Next slide shows a classification of the most important

data structures (according to some specific properties)

Unit 3- Concrete Data Types (^3)

Data Structure Classification

Concrete Versus Abstract Types

„ Concrete data types or structures (CDT's) are direct implementations of a relatively simple concept. „ Abstract Data Types (ADT's) offer a high level view (and use) of a concept independent of its implementation. „ Example: Implementing a student record: ¾ CDT: Use a struct with public data and no functions to represent the record

  • does not hide anything ¾ ADT: Use a class with private data and public functions to represent the record
  • hides structure of the data, etc. „ Concrete data structures are divided into: ¾ contiguous ¾ linked ¾ hybrid „ Some fundamental concrete data structures: ¾ arrays ¾ records, ¾ linked lists ¾ trees ¾ graphs.

Unit 3- Concrete Data Types (^7)

Arrays & Pointers

„ Are closely related. The declaration: type a[10] ¾ allocates space for 10 items of type type ¾ items are stored in consecutive memory locations

„ C++ treats consecutive elements in the array as having consecutive addresses: &a[0] < &a[1] < ... < &a[9] and &a[1] = &a[0] + 1 &a[i] = &a[i-1] + 1

„ a is a variable of type pointer to type, „ &a[i] is the same as a+i a[i] is the same as *(a+i)

„ There are two ways to access the elements of an array. We can use either: ¾ array subscripts, or ¾ pointers

„ Suppose we declare int a[10]; then

Example

„ Suppose we declare: int i, a[10] The following two statements output the elements of a

  1. for (i = 0; i < 10; i++) cout << a[i]; // using subscripts
  2. for (i = 0; i < 10; i++) cout << *(a + i); // using pointers

„ If int * p; then p = &a[i] or p = a + i makes p point to the i-th element of a.

„ Straying beyond the range of an array results in a segmentation fault. „ Pointers are not integers ¾ Exception: NULL (which is 0) can be assigned to a pointer. ¾ NULL is the undefined pointer value

Unit 3- Concrete Data Types (^9)

Dynamic arrays

„ Are declared as pointers „ Space for these arrays is allocated later, when the size is known „ Example: Consider the declarations: int b[10]; int * a; a = new int[10]; Then: ¾ a and b are both arrays ¾ b is a fixed array ; a is a dynamic array ¾ [] can also be used on a ¾ a[2] is the third element of a BUT ¾ b has space for ten integers ¾ a has space for one pointer

  • space for its elements must be allocated by new ¾ b is a constant pointer; a can be changed „ A dynamic array can be expanded ¾ I.e. to expand a we can write: int* temp = new int[10 + n]; for (int i = 0; i<10; i++) temp[i] = a[i]; delete a; a = temp;

Example Using Dynamic Arrays:

Implementation of EmployeeDB using dynamic arrays: EmployeeDB (Dynamic Array)

Passing Array Parameters

„ Arrays are always passed by reference

„ Suppose we declare,

int a[10];

To pass array a to a function f, f may be declared as:

type f( int d[], int size ) or

type f( int* d , int size)

„ In any case, f is called by f(a, sizeof a).

Unit 3- Concrete Data Types (^13)

Features of Arrays

„ Simple structures.

„ Their size is fixed;

¾ dynamic arrays can be expanded, but expansion is expensive.

„ Insertion and deletion in the middle is difficult.

„ Algorithms are simple.

„ Accessing the i-th element is very efficient

C++ Records (struct's)

„ Records allow us to group data and use them together as a unit „ The record type has: ¾ a collection of objects of same or different type ¾ each object has a unique name ¾ .obname accesses the object with name obname. „ C++ uses "struct" for records. They are also called "structures" in C++. „ For instance, after declaring struct date { int day; char* month; int year; }; date is now a new type; it can be used as: date today = {20, "jun" , 1993}; „ We can access the components of a structure using the select member operator ".“ E.g. today.month[2] // 'n'

A C++ struct may also have function members.

The difference between classes and records: by default, a class components are private, while a struct's components are public

Unit 3- Concrete Data Types (^15)

C++ Records (cont’)

„ Structures are commonly used to implement lists, trees, etc. An item of these types of structures usually looks like: struct item { int data; item* next; } ; „ We can then declare: item item1, item2, *head, *current;

¾ The physical structure of this would look like the following:

The operator ->

„ Structure components can be accessed by pointers using the point at member operator "->". „ E.g. If we set head = &item then head -> data is the data field of item. „ Structures can be copied member-wise : item2 = *head

„ We can also pass a structure as a parameter to a function. However, it is usually more efficient to pass a structure by reference, or to pass a pointer to the structure instead. „ i.e. void f(const date& d ) or void f(const date* d )

Unit 3- Concrete Data Types (^19)

Head Nodes

„ Processing of this first node is different from processing

of the other nodes.

„ A head node is a dummy node at the beginning of the

list.

¾ It is similar to the other nodes, except that it has a special value ¾ It is never deleted. ¾ Processing every actual node is the same.

„ Usually, it is more confusing and it is not used.

Circular Linked Lists (or rings)

„ A circular linked list looks like:

„ A circular linked list is appropriate when there is no

distinct first and last item.

„ The algorithms for circular linked lists are similar to

those for singly linked lists, except that

¾ none of the links is null ¾ the end of the list is reached when curr->next == head

Unit 3- Concrete Data Types (^21)

Doubly-linked Lists

„ Similar to singly linked lists except that each node also has a pointer to the previous node. „ Doubly linked list node definition: struct dnode { TYPE item; dnode* next; dnode* prev; } ; „ Operations are defined similarly

A Doubly Linked

List Toolkit :

Can be found in

Doubly Linked List

Features of Linked Lists

Compared to arrays, linked lists have the following

advantages/disadvantages:

„ Advantages

¾ Are dynamic structures; space is allocated as required. ¾ Their size is not fixed; it grows as needed. ¾ Insertion and deletion in the middle is easy.

„ Disadvantages

¾ More space is needed for the links. ¾ Algorithms are more complex. ¾ Impossible to directly access a node of the list.

Unit 3- Concrete Data Types (^25)

„ Depth or level of a node N

¾ length of the unique path from the root to N ¾ the level of the root is 1.

„ Height of a node N:

¾ length of the longest path from N to a leaf ¾ a leaf's height is 1.

„ Height of the tree:

¾ height of its root

„ The number of nodes in a binary tree of height h is >= h

and <= 2h -1 nodes.

Implementation of Trees

„ Implementation of a binary tree in C++:

¾ a node in the tree contains the item and two pointers to the subtrees:

typedef int TYPE ;

struct bnode {

TYPE item;

bnode* left;

bnode* right;

„ A C++ binary search tree is just a pointer to the root.

Unit 3- Concrete Data Types (^27)

Common Operations for Binary Trees

„ Insert an item in the tree : To the left or right of a node: ¾ insert_left : insert item on the left of a given node ¾ insert_right : insert item on the right of a given node „ find : finds the node in the tree with a given item „ find_parent : finds the parent of a given node in the tree „ delete_node: removes the node with the given item from the tree „ print: prints the whole tree (sideways)

A Binary Tree Toolkit „ An implementation of a module (or toolkit) for the binary tree structure can be found in the Examples: ¾ Binary Tree

Traversing a binary tree

„ There are three types of traversal. ¾ preorder : node then left subtree then right subtree ¾ inorder : left subtree then node then right subtree ¾ postorder : left subtree then right subtree then node

„ Inorder traversal : The following code applies a function visit to every node in the tree inorder:

void inorder( bnode root )* { // apply the function visit to every node in the tree, inorder if( root != NULL ) { inorder( root->left); visit ( root ); // apply visit to the root of the tree inorder( root->right); } } „ Tree traversal is not usually implemented by a function. What is shown here is just an example.