Overview of Data Structure
Overview of Data Structure
Data items are represented within a computer as a sequence of binary digits. These
sequences can appear very similar but have different meanings since computers can store and
manipulate different types of data.
Programming languages commonly provide data types as part of the language. These
data types are known as primitives:
• Simple Data Types – It consists of values that are in the most basic form and cannot be
decomposed into smaller parts.
Examples: Integer and real types because they are consisted of single numeric values.
• Complex Data Types – are constructed of multiple components consisting of simple types
or other complex types.
Example: objects, strings, list, and dictionaries in Python that contains multiple values.
There are cases that primitive types are not sufficient in solving large complex problems.
Thus, most of the programming languages allow for the construction of additional data type known
as user-defined types. They are defined by the programmer.
We define data structures a way in which certain data elements are logically related. It is a
class of data that can be characterized by its organization and the operation that are defined on
it. The logical relationship imposed by a data structure on the data elements implies that certain
operations may be applied on the elements, but not others. Thus the notion of a data structure
involves:
• a set of data elements (sometimes called data object –a term referring to a set of
elements).
• the way the data elements are logically related.
• a set of allowable operations on the elements of the data object.
Data structures can be viewed in 3 distinct ways, each levels being different from one another.
On this level, we try to picture the organization of the data structure and specify general
accessing procedures and functions.
Implementation Level
From the word implement, we try to examine ways to represent the data elements in
memory and to implement the accessing procedures and functions using a particular
programming language.
The last level examines the data structure in a case study in which the data structure
accurately represents the relationships in the data.
Data structure is divided into 3 different categories, the first being the primitive type or that
cannot be subdivided. The other two are formed by combining lower data structures.
Binary N-ary
B *, B+ Trees
An abstract data type (or ADT) is a programmer-defined data type that specifies a set of
data values and a collection of well-defined operations that can be performed on those values.
Abstract data types are defined independent of their implementation known as information
hiding. The information hiding separates the interaction with the ADTs though an interface or
defined sets of operations.
The four categories of set of operations
Example
x = ‘hello world’
y = x.upper()
print(y)
Example
myList = [1,2,3,4,5]
myList.reverse()
print(myList)
Important Definitions
Collection
Container
Any data structure or abstract data type that stores and organizes a collection. The
individual values of the collection are known as elements of the container and a container with no
elements is said to be empty. Python provides a number of built-in containers, which include
strings, tuples, lists, dictionaries, and sets.
Sequence
A container in which the elements are arranged in linear order from front to back, with
each element accessible by position. Python provides two immutable sequences, strings and
tuples, and one mutable sequence, the list.
Sorted Sequence
One in which the position of the elements is based on a prescribed relationship between
each element and its successor. For example, we can create a sorted sequence of integers in
which the elements are arranged in ascending or increasing order from smallest to largest value.
There are four collection data types in the Python programming language:
List
A list is a collection which is ordered and changeable. In Python lists are written with
square brackets.
▪ The list items can be access using the index.
print(myList[1])
▪ The value of specific item can be changed by referring also to the index.
print(mySubjects)
numbers = [1,18,34,3,4,10,9]
print(num)
if “apple” in myFruits:
▪ The len() method is used to determine the length of items in the list.
print(len(myFruits))
print(myFruits)
clear() Removes all the elements from fruits = ['apple', 'banana', 'cherry',
the list 'orange']
fruits.clear()
x = fruits.copy()
fruits.extend(cars)
index() Returns the index of the first fruits = ['apple', 'banana', 'cherry']
element with the specified value
x = fruits.index("cherry")
remove() Removes the item with the fruits = ['apple', 'banana', 'cherry']
specified value
fruits.remove("banana")
reverse() Reverses the order of the list fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
cars.sort()
Tuple
A tuple is a collection which is ordered and unchangeable. In Python tuples are written
with round brackets.
print(mySubjects)
▪ The items in in a tuple can be accessed by referring to the index number, inside a square
bracket.
print(mySubjects[0])
print(subject)
if “Math” in mySubjects:
print(len(mySubjects))
x = thistuple.count(5)
print(x)
print(x)
Set
A set is a collection which is unordered and unindexed. In Python sets are written with
curly brackets.
Sample Code: Creating a Set. Since the set is undored and unindexed the items will appear
randomly.
print(mySubjects)
The items in the list cannot be accessed by referring to the index since its unordered and
unindexed. But the for loop can be used to loop through set items and the in keyword to check if
the item exists.
Sample Code: The first code loops through the items in set and the second checks if the
item exists in the list.
print(fruit)
Once a set is created, you cannot change its items, but you can add new items. In adding
items to a set, the add() method is used to add one item and to add more items update() method
is used.
fruits.add(“orange”)
print(fruits)
Sample Code: Add multiple items to a set.
print(fruits)
print(len(fruits))
print(fruits)
Set Methods
Method Description
difference_update() Removes the items in this set that are also included
in another, specified set
update() Update the set with the union of this set and others
Dictionary
print(car)
There are two ways the items can be accessed of a dictionary by referring its key name
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car[“model”]
print(x)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.get(“model”)
print(x)
The value of a specific item can be changed by referring to its key name
car[“1964”]= 2018
print(car)
The for loop is used through a dictionary. When looping through a dictionary, the return
value are the keys of the dictionary, but there are methods to return the values as well.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in car:
print(x)
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in car:
print(car[x])
You can also use the values() function to return values of a dictionary:
Sample Code: Returns the values.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in car.values():
print(x)
You can also return both the keys and values of a dictionary, by using the items() function.
Sample Code: Returns the the keys and values using the items() funciton
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(x,y)
Sample Code: Returns the the keys and values using the items() funciton
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if “model” in cars:
Sample Code: Returns the the keys and values using the items() funciton
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(len(car))
The items can be added to a dictionary by using new index key and assigning a value to it.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car[“color”] = “red”
print(car)
The items can be removed from a dictionary pop(), popitem(), and del.
The pop() method removes the item with the specified key name.
Sample Code
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.pop(“model”)
print(car)
The popitem() method removes the last inserted item (in versions before 3.7, a random item is
removed instead).
Sample Code
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.popitem(“model”)
print(car)
The del keyword removes the item with the specified key name:
Sample Code
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del car[“model”]
print(car)
Sample Code: The print() function will cause an error since the enrite dictionary is
removed.
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del car
print(car)
Sample Code
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)
print(car)