First Year BA (IT)
CT1120 Algorithms
Lecture 15
Dr. Zia Ush Shamszaman
z.shamszaman1@nuigalway.ie
1
School of Computer Science, College of Science and Engineering
31-01-2020
Overview
•  Data Structure
–  Stack
–  Queue
–  Tree
•  Reviewing previous lectures
•  Feedback and Assessment
231-01-2020
3
Data Structure Concepts
•  Data Structures are containers:
–  they hold other data
–  arrays are a data structure
–  ... so are lists
•  Other types of data structures:
–  stack, queue, tree,
binary search tree, hash table,
dictionary or map, set, and on and on
•  Different types of data structures are
optimized for certain types of operations
31-01-2020
Basic Data Structure
Basic Data Structures
Linear Data Structures Non-Linear Data Structures
Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables
31-01-2020 4
Array
Linked list
Tree
Queue
Stack
Linear Data Structure
31-01-2020 5
6
Data Structure Operations
•  Data Structures will have 3 core operations
–  a way to add things
–  a way to remove things
–  a way to access things
•  Details of these operations depend on the data
structure
–  Example: List, add at the end, access by
location, remove by location
•  More operations added depending on what data
structure is designed to do
31-01-2020
Data Structure Operations
Ø  Traversing: Accessing each record exactly once so that certain
items in the record may be processed.
Ø  Searching: Finding the location of the record with a given key value,
or finding the locations of all records which satisfy one or more
conditions.
Ø  Insertion: Adding a new record to the structure.
Ø  Deletion: Removing a record from the structure.
Following two are special operations:
Ø  Sorting: Arranging the records in some logical order.
Ø  Merging: Combining the records in two different sorted files into a
single sorted file.
31-01-2020 7
Selection of Data Structure
•  The choice of particular data model depends
on two consideration:
–  It must be rich enough in structure to
represent the relationship between data
elements
–  The structure should be simple enough that
one can effectively process the data when
necessary
31-01-2020 8
Stack
•  A Stack is a list of elements in which an
element may be inserted or deleted at
one end which is known as TOP of the
stack.
31-01-2020 9
Stack
•  Collection with access only to the last
element inserted
•  Last in First out (LIFO)
•  Insert/Push
•  Remove/Pop
•  Top
•  Make empty
TopData4
Data3
Data2
Data1
push
pop
31-01-2020 10
Stack Operation
31-01-2020 11
•  Stack() creates a new stack that is empty. It needs no
parameters and returns an empty stack.
•  push(item) adds a new item to the top of the stack. It needs
the item and returns nothing.
•  pop() removes the top item from the stack. It needs no
parameters and returns the item. The stack is modified.
•  peek() returns the top item from the stack but does not
remove it. It needs no parameters. The stack is not
modified.
•  isEmpty() tests to see whether the stack is empty. It needs
no parameters and returns a boolean value.
•  size() returns the number of items on the stack. It needs no
parameters and returns an integer.
31-01-2020 12
Example
from pythonds.basic import Stack
s=Stack()
print(s.isEmpty())
s.push(4)
s.push('dog')
print(s.peek())
s.push(True)
print(s.size())
print(s.isEmpty())
s.push(8.4)
print(s.pop())
print(s.pop())
print(s.size())
31-01-2020 13
True
dog
3
False
8.4
True
2
Output
Queue
•  A queue is an ordered collection of items where
the addition of new items happens at one end,
called the “rear,” and the removal of existing
items occurs at the other end, commonly called
the “front.”
•  As an element enters the queue it starts at the
rear and makes its way toward the front, waiting
until that time when it is the next element to be
removed..
31-01-2020 14
Queue
•  Collection with access only to the item that has
been present the longest
•  Last in Last Out (LILO)/First in First Out(FIFO)
•  enqueue, dequeue, front
•  priority queues and dequeue
31-01-2020 15
Basic Operation
•  Queue() creates a new queue that is empty. It needs no
parameters and returns an empty queue.
•  enqueue(item) adds a new item to the rear of the
queue. It needs the item and returns nothing.
•  dequeue() removes the front item from the queue. It
needs no parameters and returns the item. The queue
is modified.
•  isEmpty() tests to see whether the queue is empty. It
needs no parameters and returns a boolean value.
•  size() returns the number of items in the queue. It
needs no parameters and returns an integer.
31-01-2020 16
31-01-2020 17
Example
q=Queue()
q.enqueue(4)
q.enqueue('dog')
q.enqueue(True)
print(q.size())
31-01-2020 18
Tree
•  A Tree is a collection of elements called nodes.
•  One of the node is distinguished as a root, along with a
relation (“parenthood”) that places a hierarchical
structure on the nodes.
31-01-2020 19
Vocabulary & Definitions
•  Node
–  A node is a fundamental part of a tree. It can have a
name, which we call the “key.” A node may also have
additional information. We call this additional information
the “payload.” While the payload information is not
central to many tree algorithms, it is often critical in
applications that make use of trees.
•  Edge
–  An edge is another fundamental part of a tree. An edge
connects two nodes to show that there is a relationship
between them. Every node (except the root) is connected
by exactly one incoming edge from another node. Each
node may have several outgoing edges.
•  Root
–  The root of the tree is the only node in the tree that has
no incoming edges.
31-01-2020 20
Vocabulary & Definitions
•  Path
–  A path is an ordered list of nodes that are connected by
edges. For example, Mammal → Carnivora → Felidae →
Felis → Domestica is a path.
•  Children
–  The set of nodes 𝑐 that have incoming edges from the
same node to are said to be the children of that node.
•  Parent
–  A node is the parent of all the nodes it connects to with
outgoing edges.
31-01-2020 21
Vocabulary & Definitions
•  Sibling
–  Nodes in the tree that are children of the same parent
are said to be siblings.
•  Subtree
–  A subtree is a set of nodes and edges comprised of a
parent and all the descendants of that parent.
•  Leaf Node
–  A leaf node is a node that has no children. For example,
Human and Chimpanzee are leaf nodes
31-01-2020 22
Next Lecture
Class test
Syllabus (all three lectures)
31-01-2020 23
Useful Links	
•  http://www.csanimated.com/animation.php?t=Quicksort
•  http://www.hakansozer.com/category/c/
•  http://www.nczonline.net/blog/2012/11/27/computer-science-in-
javascript-quicksort/
•  http://www.sorting-algorithms.com/shell-sort
•  https://www.tutorialspoint.com/
•  https://www.hackerearth.com/
•  www.khanacademy.org/computing/computer-science/algorithms
31-01-2020 24
Feedback & Assessment
31-01-2020 25

L 15 ct1120

  • 1.
    First Year BA(IT) CT1120 Algorithms Lecture 15 Dr. Zia Ush Shamszaman z.shamszaman1@nuigalway.ie 1 School of Computer Science, College of Science and Engineering 31-01-2020
  • 2.
    Overview •  Data Structure – Stack –  Queue –  Tree •  Reviewing previous lectures •  Feedback and Assessment 231-01-2020
  • 3.
    3 Data Structure Concepts • Data Structures are containers: –  they hold other data –  arrays are a data structure –  ... so are lists •  Other types of data structures: –  stack, queue, tree, binary search tree, hash table, dictionary or map, set, and on and on •  Different types of data structures are optimized for certain types of operations 31-01-2020
  • 4.
    Basic Data Structure BasicData Structures Linear Data Structures Non-Linear Data Structures Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables 31-01-2020 4
  • 5.
  • 6.
    6 Data Structure Operations • Data Structures will have 3 core operations –  a way to add things –  a way to remove things –  a way to access things •  Details of these operations depend on the data structure –  Example: List, add at the end, access by location, remove by location •  More operations added depending on what data structure is designed to do 31-01-2020
  • 7.
    Data Structure Operations Ø Traversing: Accessing each record exactly once so that certain items in the record may be processed. Ø  Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions. Ø  Insertion: Adding a new record to the structure. Ø  Deletion: Removing a record from the structure. Following two are special operations: Ø  Sorting: Arranging the records in some logical order. Ø  Merging: Combining the records in two different sorted files into a single sorted file. 31-01-2020 7
  • 8.
    Selection of DataStructure •  The choice of particular data model depends on two consideration: –  It must be rich enough in structure to represent the relationship between data elements –  The structure should be simple enough that one can effectively process the data when necessary 31-01-2020 8
  • 9.
    Stack •  A Stackis a list of elements in which an element may be inserted or deleted at one end which is known as TOP of the stack. 31-01-2020 9
  • 10.
    Stack •  Collection withaccess only to the last element inserted •  Last in First out (LIFO) •  Insert/Push •  Remove/Pop •  Top •  Make empty TopData4 Data3 Data2 Data1 push pop 31-01-2020 10
  • 11.
    Stack Operation 31-01-2020 11 • Stack() creates a new stack that is empty. It needs no parameters and returns an empty stack. •  push(item) adds a new item to the top of the stack. It needs the item and returns nothing. •  pop() removes the top item from the stack. It needs no parameters and returns the item. The stack is modified. •  peek() returns the top item from the stack but does not remove it. It needs no parameters. The stack is not modified. •  isEmpty() tests to see whether the stack is empty. It needs no parameters and returns a boolean value. •  size() returns the number of items on the stack. It needs no parameters and returns an integer.
  • 12.
  • 13.
    Example from pythonds.basic importStack s=Stack() print(s.isEmpty()) s.push(4) s.push('dog') print(s.peek()) s.push(True) print(s.size()) print(s.isEmpty()) s.push(8.4) print(s.pop()) print(s.pop()) print(s.size()) 31-01-2020 13 True dog 3 False 8.4 True 2 Output
  • 14.
    Queue •  A queueis an ordered collection of items where the addition of new items happens at one end, called the “rear,” and the removal of existing items occurs at the other end, commonly called the “front.” •  As an element enters the queue it starts at the rear and makes its way toward the front, waiting until that time when it is the next element to be removed.. 31-01-2020 14
  • 15.
    Queue •  Collection withaccess only to the item that has been present the longest •  Last in Last Out (LILO)/First in First Out(FIFO) •  enqueue, dequeue, front •  priority queues and dequeue 31-01-2020 15
  • 16.
    Basic Operation •  Queue()creates a new queue that is empty. It needs no parameters and returns an empty queue. •  enqueue(item) adds a new item to the rear of the queue. It needs the item and returns nothing. •  dequeue() removes the front item from the queue. It needs no parameters and returns the item. The queue is modified. •  isEmpty() tests to see whether the queue is empty. It needs no parameters and returns a boolean value. •  size() returns the number of items in the queue. It needs no parameters and returns an integer. 31-01-2020 16
  • 17.
  • 18.
  • 19.
    Tree •  A Treeis a collection of elements called nodes. •  One of the node is distinguished as a root, along with a relation (“parenthood”) that places a hierarchical structure on the nodes. 31-01-2020 19
  • 20.
    Vocabulary & Definitions • Node –  A node is a fundamental part of a tree. It can have a name, which we call the “key.” A node may also have additional information. We call this additional information the “payload.” While the payload information is not central to many tree algorithms, it is often critical in applications that make use of trees. •  Edge –  An edge is another fundamental part of a tree. An edge connects two nodes to show that there is a relationship between them. Every node (except the root) is connected by exactly one incoming edge from another node. Each node may have several outgoing edges. •  Root –  The root of the tree is the only node in the tree that has no incoming edges. 31-01-2020 20
  • 21.
    Vocabulary & Definitions • Path –  A path is an ordered list of nodes that are connected by edges. For example, Mammal → Carnivora → Felidae → Felis → Domestica is a path. •  Children –  The set of nodes 𝑐 that have incoming edges from the same node to are said to be the children of that node. •  Parent –  A node is the parent of all the nodes it connects to with outgoing edges. 31-01-2020 21
  • 22.
    Vocabulary & Definitions • Sibling –  Nodes in the tree that are children of the same parent are said to be siblings. •  Subtree –  A subtree is a set of nodes and edges comprised of a parent and all the descendants of that parent. •  Leaf Node –  A leaf node is a node that has no children. For example, Human and Chimpanzee are leaf nodes 31-01-2020 22
  • 23.
    Next Lecture Class test Syllabus(all three lectures) 31-01-2020 23
  • 24.
    Useful Links •  http://www.csanimated.com/animation.php?t=Quicksort • http://www.hakansozer.com/category/c/ •  http://www.nczonline.net/blog/2012/11/27/computer-science-in- javascript-quicksort/ •  http://www.sorting-algorithms.com/shell-sort •  https://www.tutorialspoint.com/ •  https://www.hackerearth.com/ •  www.khanacademy.org/computing/computer-science/algorithms 31-01-2020 24
  • 25.