









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Most Important Concrete Data Structures. ➢Arrays. ➢Records. ➢Linked Lists. ➢Binary Trees. Unit 3- Concrete Data Types. 2. Overview of Data Structures.
Typology: Lecture notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Classification of Data Structures Concrete vs. Abstract Data Structures Most Important Concrete Data Structures ¾Arrays ¾Records ¾Linked Lists ¾Binary Trees
¾ simple or atomic ¾ structured data types or data structures
¾ a number of components ¾ a structure ¾ a set of operations
Unit 3- Concrete Data Types (^3)
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
Unit 3- Concrete Data Types (^7)
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
Suppose we declare: int i, a[10] The following two statements output the elements of a
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)
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
Example Using Dynamic Arrays:
Implementation of EmployeeDB using dynamic arrays: EmployeeDB (Dynamic Array)
Unit 3- Concrete Data Types (^13)
¾ dynamic arrays can be expanded, but expansion is expensive.
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)
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:
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)
¾ 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.
¾ none of the links is null ¾ the end of the list is reached when curr->next == head
Unit 3- Concrete Data Types (^21)
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
¾ 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.
¾ 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)
¾ length of the unique path from the root to N ¾ the level of the root is 1.
¾ length of the longest path from N to a leaf ¾ a leaf's height is 1.
¾ height of its root
¾ a node in the tree contains the item and two pointers to the subtrees:
Unit 3- Concrete Data Types (^27)
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
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.