![Page 1: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/1.jpg)
![Page 2: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/2.jpg)
Do you remember what is the most powerful tool for managing complexity?
2
Abstraction!
![Page 3: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/3.jpg)
Abstractions and more abstractions …
3You are here
![Page 4: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/4.jpg)
8.1 Three views of data
4
Application or user level
Logical or abstract level
Implementation level
Abstract Data Types (ADTs)
are here!
The Data Structures from Ch.7 are here!
Applications, like spreadsheets or
robotics (Chs.12, 13, 14) are here!
![Page 5: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/5.jpg)
Abstract Data Type (ADT)
Abstract data type = A data structure whose properties (data and operations) are specified independently of any particular implementation.
5
Python strings and lists have their own methods attached to them, e.g. my_list.sort( )
… but we do not know how exactly they are stored in
memory, or what algorithm is used for sorting!
![Page 6: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/6.jpg)
Abstract Data Type (ADT)
An abstract data type is also called container or black-box.
6
![Page 7: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/7.jpg)
8.2 Stacks
Stack = An ADT in which accesses are made at only one end
– LIFO = Last In First Out
– The insert is called Push and the removal is called Pop
7
Name three everyday objects that operate as stacks!
![Page 8: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/8.jpg)
Stack example:Towers of Hanoi
8
Tower of Hanoi - Wikipedia, the free encyclopedia
![Page 9: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/9.jpg)
9
Stack algorithmsPlacing elements in the stack:
WHILE (more data)Read valuepush(myStack, value)
Removing elements from the stack:
WHILE (NOT IsEmpty(myStack))value = pop(myStack)Write value
![Page 10: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/10.jpg)
ExampleA stack is initially empty. Draw the stack after
each of these operations:• push(42)• push(15)• push(10)• pop()• push(21)• pop()• pop()
10
42 4215
10
4215
4215
421521
4215
42
![Page 11: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/11.jpg)
QUIZA stack contains initially only the number 42. Draw
the stack at the end of each operation:• push(10)• push(11)• pop()• push(12)• pop()• push(101)• push(102)• pop()
11
42
![Page 12: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/12.jpg)
Solution• push(10)• push(11)• pop()• push(12)• pop()• push(101)• push(102)• pop()
12
1011042
This is the stack at the end:
![Page 13: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/13.jpg)
8.3 Queues
Queue = An ADT in which items are inserted at one end and removed from the other end
– FIFO = First In First Out
– No standard queue terminology
• Enqueue, Enque, Enq, Enter, and Insertare used for the insertion operation
• Dequeue, Deque, Deq, Delete, and Removeare used for the removal operation.
13
Name three
everydaystructuresthat arequeues
![Page 14: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/14.jpg)
QUIZA queue is initially empty. Draw the queue after
each of these operations:• enque(42)• enque(15)• enque(10)• deque()• enque(21)• deque()• deque()
14
42
4215
421510
1510
![Page 15: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/15.jpg)
Interesting trivia: queueing is one of only two words in the English language that has 5 vowels in a row!
E.g. Queueing Theory
Alternative spelling: queing
15
The other one is miaouing (miaoued?)
![Page 16: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/16.jpg)
Queue algorithmsPlacing elements in the queue:
WHILE (more data)Read valueenque(myQueue, value)
Removing elements from the queue:
WHILE (NOT IsEmpty(myQueue))value = deque(myQueue)Write value
![Page 17: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/17.jpg)
Two implementations for ADTsArray-based implementationObjects in the container are kept in an array, i.e. physically next to each other in memory.
Linked-based implementationObjects in the container are not kept physically together, but each item tells you where to go to get the next one in the structure
17
Did you ever play treasure hunt, a game in which each cluetells you where to go to get the next clue?
![Page 18: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/18.jpg)
18
Memory maps forArray implementation Linked implementation
Not in text
![Page 19: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/19.jpg)
19
Stacks and Queues in the linked implementation
![Page 20: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/20.jpg)
20
Why access elements only at the ends?
![Page 21: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/21.jpg)
8.4 Lists• List are more general ADTs than stacks and queues,
because operations can be performed anywhere, not just at the ends.
• There is an index for the current item (crt) that can move through the list.– Operations are performed at position crt
21
![Page 22: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/22.jpg)
8.4 ListsNote: The text does not mention crt, but some list operations (Reset, GetNext, MoreItems) are impossible without it! (See next slide…)
22
![Page 23: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/23.jpg)
Operations that can be applied to lists:• InsertItem Insert new item into list at position crt
– The old item moves right, crt points to new item• DeleteItem Remove the crt item from the list
– Move crt right (except when at tail of list, in which case crtpoints to the new tail)
• GetNext Get the crt item– Move crt right (if at end of list set crt = NULL)
• MoreItems Are there more items after crt?• Reset Brings crt back to head of list
23
![Page 24: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/24.jpg)
QUIZ: Show the list and the outputs (if any) after each of the following operations:
getNext()getNext()delete()getNext()insert(42)moreItems()getNext() getNext()getNext()reset()
24
![Page 25: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/25.jpg)
Array implementation of a list
crt
Details in COSC 2341 - Data Structures!
Note: The text does not mention crt, but some list operations (Reset, GetNext, MoreItems) are impossible without it!
1
![Page 26: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/26.jpg)
Array implementation of a list
Big problem: insertion and deletion require shifting the “tail” of the array → time-consuming!
![Page 27: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/27.jpg)
Do not confuse the levels!
The list (ADT, Ch.8) is here!
The array (data structure, ch.7) is
here!
The Excel spreadsheet(application, Chs.12, 13, 14)
is here!
![Page 28: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/28.jpg)
Linked implementation of a list
28
crt
But, wait a sec, this is the same diagram we had for the list ADT – why are we calling it now an implementation?
A: Because most list implementations are actually linked!
![Page 29: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/29.jpg)
Algorithm for creating a list and printing its elements
29
WHILE (more data)Read valueInsert(myList, value)
Reset(myList)Write "Items in the list are "WHILE (moreItems(myList))
GetNext(myList, nextItem)Write nextItem, ' 'moveNext(myList)
Brings crt back to the head of the list
Trick question: Which implementation is being used (array or linked)?
![Page 30: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/30.jpg)
30
Logical LevelThe algorithm that uses the list does not and should not know how the data in the list is stored (array or linked), or how the various operations (Insert(), Reset(), MoreItems()) are implemented!
We have written algorithms using a stack, a queue, and a list without ever knowing the internal workings, i.e. the implementation of these ADTs/containers.
![Page 31: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/31.jpg)
8.5 TreesADTs such as lists, stacks, and queues are used to model linear relationshipsOther types of relationships require different, non-linear ADTs.
31
![Page 32: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/32.jpg)
Binary Tree (BT)
32
Root node
Node with two children
Node with right child
Leaf node
Node with left child
![Page 33: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/33.jpg)
Binary tree = An ADT/container with a unique starting node called the root, in which:• A node can have 0, 1, or 2 children• A unique path (series of nodes) exists from the root to every other node
– Equivalently, there are no loops!
33
![Page 34: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/34.jpg)
QUIZ
34
Write the (unique) path from the root to the node containing:• 7• 8• 6
![Page 35: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/35.jpg)
QUIZ
35
List all the nodes having:• 0 children• 1 child• 2 children
![Page 36: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/36.jpg)
QUIZ: Are these Trees? BTs?
36
![Page 37: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/37.jpg)
QUIZ: Are these Trees? BTs?
![Page 38: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/38.jpg)
QUIZ CONCLUSIONS: Trees• Have one (and only one) root• The edges are directional, from parent to child• There is one path from the root to any other node• The path above is unique
In addition, Binary Trees have to satisfy:• Any node can have only 0, 1, or 2 children
![Page 39: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/39.jpg)
To do for next time:
• Read pp.247-258 in our text.• End-of-chapter 1 – 9, 11 – 19, 21 – 24
39EoHW 1
![Page 40: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/40.jpg)
Putting a BT to work:Binary Search Tree (BST)
40
Do you notice a pattern in this BT?
![Page 41: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/41.jpg)
Binary Search Tree = A tree with the following properties:1. Shape property → It is a binary tree2. Semantic property→ For every node, the value
stored in the node is– greater than the value in any node in its left subtree
and– smaller than the value in any node in its right subtree
41
![Page 42: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/42.jpg)
QUIZ: Are these Binary Search Trees?
![Page 43: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/43.jpg)
Algorithm for searching an item in a BST
43
IsThere(tree, item)IF (tree is null)
RETURN FALSEELSE
IF (item equals info(tree))RETURN TRUE
ELSEIF (item < info(tree))
IsThere(left(tree), item)ELSE
IsThere(right(tree), item)
Recursive!
![Page 44: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/44.jpg)
Example:Search for item 18
44
IsThere(tree, item)IF (tree is null)
RETURN FALSEELSE
IF (item equals info(tree))RETURN TRUE
ELSEIF (item < info(tree))
IsThere(left(tree), item)ELSE
IsThere(right(tree), item)
Read the detailed explanation on pp.256-7!
![Page 45: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/45.jpg)
QUIZ• What do the acronyms BT and BST mean in
Computer Science?
• How is a BT similar to a BST?
• How is a BT different from a BST?
45
![Page 46: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/46.jpg)
Binary Search Tree = A tree with the following properties:1. Shape property → It is a binary tree2. Semantic property→ For every node, the value
stored in the node is– greater than the value in any node in its left subtree
and– smaller than the value in any node in its right subtree
46
x
<X >X
![Page 47: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/47.jpg)
47
Inserting an item in a BST: first we have to search for it!
Search for Kyrstenin this tree …
![Page 48: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/48.jpg)
48
Inserting an item in a BST: first we have to search for it!
Search for kyrstenin this tree …
… and insert the itemwhere the search ended!
![Page 49: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/49.jpg)
49
Insert(tree, item)IF (tree is null)
Put item in a new treeELSE
IF (item < info(tree))Insert (left(tree), item)
ELSEInsert (right(tree), item)
Note how similar this algorithmis to the search algorithm!
![Page 50: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/50.jpg)
QUIZ on BST insertion
Problem 47/280:The following elements are inserted in an initially empty BST:50, 72, 96, 107, 26, 12, 11, 9, 2, 10, 25, 51, 16, 17, 95Find the final tree.
50
![Page 51: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/51.jpg)
QUIZ on BST insertion
The following strings are inserted in an initially empty BST:“Hello, world!”, “COSC 1302”, “MATH 2412”, “PHYS 2425”, “ENGL 1301”, “ENGL 1302”
Show the final tree.Hint: Use dictionary order.
51
![Page 52: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/52.jpg)
Printing/traversing a BST
52
Print(tree)If (tree is not null)
Print (left(tree))Write info(tree)Print (right(tree))
Is that all there is to it? Yes!Remember that recursive
algorithms can be very powerful
![Page 53: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/53.jpg)
Printing/traversing a BST
53
Print(tree)If (tree is not null)
Print (left(tree))Write info(tree)Print (right(tree))
This is called “in-order” traversal
![Page 54: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/54.jpg)
QUIZ: Printing/traversing a BST
54
Print(tree)If (tree is not null)
Print (left(tree))Write info(tree)Print (right(tree))
Apply the algorithm to this BST. Show all output.
![Page 55: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/55.jpg)
FYI: Other tree algorithmsFinding the depth of a tree:
Depth(tree)IF (tree is null)
RETURN 0ELSE
RETURN max(Depth(left(tree), Depth(right(tree)))
Finding the length of a tree:
Length(tree)IF (tree is null)
RETURN 0ELSE
RETURN 1 + Length(left(tree))+ Length(right(tree))
![Page 56: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/56.jpg)
8.6 Graphs
Graph = A data structure that consists of a set of nodes (called vertices) and a set of edges that connect nodes to each other.
Note: The “unique path” condition from trees has been removed; graphs are more general than trees!
56
![Page 57: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/57.jpg)
Graphs: Unlike trees, there can be cycles and unconnected parts
57
![Page 58: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/58.jpg)
QUIZ: Find 5 different cycles in this graph
58
![Page 59: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/59.jpg)
QUIZ: In each case, decide if the data structure is a tree or just a graph
59
![Page 60: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/60.jpg)
Graphs: directed and undirected
60
![Page 61: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/61.jpg)
Graphs
61
![Page 62: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/62.jpg)
Representing a Graph: adjacency matrix
62
See Table 8.3/262
Our text simply calls it
“table”
![Page 63: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/63.jpg)
QUIZ: Draw the adjacency matrix for this graph
63
![Page 64: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/64.jpg)
QUIZ: Draw the adjacency matrix for this graph
64
![Page 65: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/65.jpg)
QUIZ: Draw the adjacency matrix for this graph
65EOL 3
![Page 66: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/66.jpg)
QUIZ: Draw the adjacency matrix for this graph
66
![Page 67: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/67.jpg)
Graph Algorithms
67
![Page 68: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/68.jpg)
Depth-First Searching Algorithm--Given a starting vertex and an ending vertex, we can develop an algorithm that finds a path from startVertex to endVertex
This is called a depth-first search because we start at a given vertex, we go to the deepest branch and explore as far down one path before taking alternative choices at earlier branches.
68
![Page 69: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/69.jpg)
DFS – Stack to the rescue!
69
startVertex endVertex
Push the start vertex
![Page 70: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/70.jpg)
DFS – Stack to the rescue!
70
startVertex endVertex
Pop the start vertex, then push its children (in alphabetical order)
![Page 71: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/71.jpg)
DFS – Stack to the rescue!
Figure 8.11 Using a stack to store the routes
71
startVertex endVertex
Unleash recursion!
![Page 72: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/72.jpg)
DFS – Nitty gritty: How to avoid loops?
Figure 8.11 Using a stack to store the routes
72
startVertex endVertex
Mark each node when we push it.
![Page 73: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/73.jpg)
DFS – Nitty gritty: When to stop?
Figure 8.11 Using a stack to store the routes
73
startVertex endVertex
A node is considered visited when popped (not when pushed)
![Page 74: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/74.jpg)
QUIZ DFS: Find a path from Austin to Chicago
74
![Page 75: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/75.jpg)
Depth First Search(startVertex, endVertex)Set found to FALSEPush(myStack, startVertex)WHILE (NOT IsEmpty(myStack) AND NOT found)
Pop(myStack, tempVertex)IF (tempVertex equals endVertex)
Write endVertexSet found to TRUE
ELSE IF (tempVertex not visited)Write tempVertexPush all unvisited vertices adjacent to tempVertexMark tempVertex as visited
IF (found)Write "Path has been printed"
ELSEWrite "Path does not exist")
75
And mark pushed vertex
as visited
![Page 76: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/76.jpg)
Is this the only path from Austin to Washington?
76
![Page 77: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/77.jpg)
Is this the shortest path from Austin to Washington?
77
![Page 78: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/78.jpg)
Conclusion on DFS: The algorithm is guaranteed to find a path (if at least one exists) between the given vertices• Not all paths• Not (in general) the shortest path
To find all, or the shortest path, we need to use different algorithms …
78
![Page 79: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/79.jpg)
Other Graph Algorithms
• Breadth-First Search (BFS)• Single-Source Shortest-Path
Search (SSPS)• All-Pairs Shortest Path Search• Flow algorithms• Etc. etc. etc.
79
Skip them in our text!
![Page 80: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/80.jpg)
80
SKIP8.7 Subprogram Statements
READ and take notes:Ethical issues: Workplace Monitoring
![Page 81: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/81.jpg)
Homework for Ch.8
31, 32, 34, 35, 37, 38, 47, 48, 5054 →“Table” means adjacency matrix.56 → Instead of Sandler, let the destination be Fred. The edges are all bidirectional (they go both ways). Remember that we push nodes on the stack in alphabetical order!
81
![Page 82: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/82.jpg)
Chapter Review Questions
• Distinguish between an array-based implementation and a linked implementation
• Distinguish between an array and a list• Distinguish between and a unsorted list and a sorted
list• Distinguish between the behavior of a stack and a
queue• Distinguish between the binary tree and a binary
search tree
82
![Page 83: Do you remember what is the most complexity?...Abstract Data Type (ADT) Abstract data type = A data structure whose properties (data and operations) are specified independently of](https://reader034.vdocument.in/reader034/viewer/2022042808/5f8484c50a20a30a0d46fecf/html5/thumbnails/83.jpg)
Chapter Review Questions
• Draw the binary search tree that is built from inserting a series of items
• Understand the difference between a tree and a graph
• Use the DFS algorithm to find a path between two nodes of a graph.
83