Download presentation
Presentation is loading. Please wait.
1
Lesson Objectives Aims
Understand algorithms for the following data structures: Stack Queue Tree Depth first traversal Breadth first traversal Linked List
2
This lesson Tough though it sounds, you are going to need to learn the code for a lot of algorithms You’re going to need to be able to write and recognise that code. It WILL be in your exam – wait until we look at the past papers for proof of that. If you don’t do the tasks in this lesson you will find this incredibly difficult and depressing in equal measure.
3
Notes CS is about NOT re-inventing the wheel.
There is a folder in shared called “Princeton University Notes” These are EXCELLENT resources – they contain diagrams, explanations, examples and CODE (in Java, suck it up) which are INVALUABLE to you. Were there enough caps there for emphasis??
4
A stack is a FILO data structure
Stacks A stack is a FILO data structure Imagine a pile of plates – which one can you get next (realistically?) Stacks support two main operations Push – put something on the top of the stack Pop – get the first item from the top of the stack
5
Stacks Stacks are incredibly useful when we want to remember the order we did something in or return to a previous state: CPU states (interrupts) Undo (in a program) Reversal of a list of items
6
Task Implement a Stack class in VB which makes a stack of strings. It must have the following methods: Push Pop GetStackSize (returns the number of elements in the stack) IsFull (returns true or false) IsEmpty (returns true or false) The constructor should set the maximum size of the stack. Print your code and an example of it working and add it to your notes.
7
Queue A queue is a FIFO data structure
New data is added to the end or “tail” of the queue Data is removed from the front or “head” of the queue As with stacks, we use a pointer to keep track of where the head and tail of the queue are.
8
Queue Like a stack, queues are of finite size (usually) and therefore must be checked for their empty/full states when adding or removing data.
9
Task Implement a Queue class in VB which accepts a queue of Integers. It must have the following methods: Enqueue Dequeue IsFull (returns true or false) IsEmpty (returns true or false) The constructor should set the maximum size of the queue. If you use arrays, think about how you will use the empty elements – MOD? Print your code and an example of it working and add it to your notes.
10
A linked list is formed from Nodes. A node contains:
The data A pointer to the next node In reality, when you code it, the node may also contain an ID of some sort (if your language doesn’t support pointers, how else are you going to link the list?!)
11
Linked lists
12
Bound by the size of the memory or allocation for nodes
Linked lists Dynamic Bound by the size of the memory or allocation for nodes Can remove a node by simply changing the pointer Could be expanded by: Creating links for different purposes Double linked lists??
13
Task Code a linked list of strings in VB VB does not have pointers!
So… You will need to create a STRUCTURE which contains the data and a pointer. I’d also suggest it contains an ID? Or you could use the index of an array to be the pointers? Your class should contain: Add node Remove node Fetch nodes (show the list)
14
Tree A tree is a data structure made up of “Nodes”
A node is simply a point in the tree which holds: Some data A pointer to the left node A pointer to the right node A pointer may be: Another node Null (it doesn’t exist)
15
Terminology (all are nodes (the same thing)):
What does it look like? Terminology (all are nodes (the same thing)): ROOT – The top node in the tree. All trees have one (and only one) root node. LEAF – A single node which isn’t the root, but has no children. PARENT – A node which isn’t the root, but does have one or more children CHILD – A node connected to a parent node.
16
Tree Vs Binary Tree Most tree data structures are implemented as “Binary Trees” These are identical, except for one important feature Each node may have a maximum of 2 children – a left and a right node.
17
Task I am not going to ask you to implement a tree
BUT, I am going to ask you to type in (not download and run) the code linked on the next slide You STILL need to understand the code It is also an excellent introduction to recursion and makes a lot of sense.
18
Trees A really good resource
19
Traversal Trees are often used because they: Automatically sort data
Make sense for AI: Decisions Following trains of thought Potential moves in a game etc. They can be “traversed” (searched) in multiple ways. Find out about Depth first and breadth first (fairly obvious) and add to your notes. You will have already covered one if you tried the code and tested it.
20
Review/Success Criteria
You should know: The main data structures in computer science How to recognise a data structure from its code How to code each data structure in VB and, by default, pseudo code
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.