Upload
others
View
2
Download
0
Embed Size (px)
Citation preview
1
QueuesQueue An abstract data type in which items are entered at one end and removed from the other end
– FIFO, for First In First Out
– No standard queue terminology• Enqueue, Enque, Enq, Enter, and Insert
are used for the insertion operation• Dequeue, Deque, Deq, Delete, and Remove
are used for the deletion operation.
Name three
everydaystructuresthat arequeues
● These are similar to queue we are while waiting for lunch or any other thing.
● The first person who comes to queue will be the first one to be served or get out of the queue.
● The first item entering the queue will get out first.● Enqueue(item) - to put items in the list● Dequeue() - to remove the first item from list.
What the queue should look like after following instructions:
enqueue(10)
Queue: 10
enqueue(20)
Queue: 10 20
enqueue(13)
Queue: 10 20 13
enqueue(22)
Queue: 10 20 13 22
print dequeue() - Output will be 10 as it is the first element and will be removed.
Queue: 20 13 22
enqueue(32)
Queue: 20 13 22 32
dequeue() - Removes the first element but no print
Queue: 13 22 32
print dequeue()
Output: 13
Queue: 22 32
enqueue(89)
22 32 89
5
StacksStack
An abstract data type in which accesses are made at only one end
– LIFO, which stands for Last In First Out
– The insert is called Push and the delete is called Pop Name three everyday
structures that are stacks
● A stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the item into the stack, and pop the item out of the stack.
● Wherever you put last in the stack will come first.● You can see it as a stack of books. The first book you place goes to the
bottom, then another book above it. If we get the books out of stack, we need to remove the last book we put on that stack.
● push(item) - put item in the stack● pop() - get item from the stack.
What does the stack should look like after these instructions
push(10)
push(12)
push(13)
print pop()
Output: 13
10
12
10
13
12
10
12
10
push(23)
push(05)
pop()
23
12
10
05
23
12
10
23
12
10
print pop()
Output: 23
print pop()
Output: 12
print pop()
Output: 10
● Now the stack is empty and we can not pop any more items. We need to push items in stack before we can pop.
12
10
10
11
TreesBinary treeA linked container with a unique starting node called the root, in which each node is capable of having two child nodes, and in which a unique path (series of nodes) exists from the root to every other node
A picture is worth athousands words…
12
TreesRoot node
Node with two children
Node with right child
Leaf node
Node with left childWhat is the unique path to the node containing
5? 9? 7? …
13
Binary Search TreesBinary search tree (BST)A binary tree (shape property) that has the (semantic) property that characterizes the values in a node of a tree:
The value in any node is greater than the value in any node in its left subtree and less than the value in any node in its right subtree.
14
Binary Search Tree
Since all the nodes in left are
smaller and right are larger, it can be used
to perform binary search.
Each nodeis the root
of a subtreemade up ofits left and
right children
15
Binary Search Tree
Trace the nodes passedas you searchfor 18, 8, 5,4, 9, and 15
Let's search for 18 in the following tree. ● First, compare 18 with 15 which is the root.● Since 18 > 15, we should look to the right.● Compare 17 to 18, since 18 is larger we move
to the right.● The right node is 19. Then we compare 18 and
19. Since 18 < 19, we move to left.● Then we compare 18 and 18. And found it!!!
● If you reach the leaf node and did not found the searched item, it means that the item is not in the tree.
● To search 18, we need to pass 15, 17, 19 and 18 nodes.
Creating Binary Search TreesEx. Create a binary search tree made of following numbers 8, 12, 15, 5, 3, 14, 10, 6
● Insert the first element on the list as root.
● Now take the second element on the list is 12. We start from the root and check where the new item should go in the tree. In this case, 12 > 8, so it should go to the right of 8.
● Now take the next element on the list is 15. We start from the root and check where the new item should go in the tree. In this case, 15 > 8, so it should go to the right of 8. But, there is 12 at the right of 8, now we compare 12 with 15. Since 15 > 12, 15 should go to the right of 12.
8
8
12
Creating Binary Search Tree8
12
15
● Now take the next element on the list is 5. We start from the root and check where the new item should go in the tree. In this case, 5 < 8, so it should go to the left of 8.
8
12
15
5
Creating Binary Search Tree● Now take the next element on the list is 3. We start from the root and check where the new item
should go in the tree. In this case, 3 < 8, so it should go to the left of 8. But, there is 5 at the left of 8, now we compare 3 with 5. Since 3 < 5, 3 should go to the left of 5.
8
12
15
5
3
Creating Binary Search Tree● Now take the next element on the list is 14. We start from the root and check where the new item
should go in the tree. In this case, 14 > 8, so it should go to the right of 8. But, there is 12 at the right of 8, now we compare 14 with 12. Since 14 > 12, 14 should go to the right of 12. But again, there is 15 there, so we compare 15 with 14. Since 14 < 15, 14 should go to the left of 15.
8
12
15
5
3
14
Creating Binary Search Tree● Now take the next element on the list is 10. We start from the root and check where the new item
should go in the tree. In this case, 10 > 8, so it should go to the right of 8. But, there is 12 at the right of 8, now we compare 10 with 12. Since 10 < 12, 10 should go to the left of 12.
8
12
15
5
3
14
10
Creating Binary Search Tree● Now take the next element on the list is 6. We start from the root and check where the new item
should go in the tree. In this case, 6 < 8, so it should go to the left of 8. But, there is 5 at the left of 8, now we compare 6 with 5. Since 6 > 5, 6 should go to the right of 5.
8
12
15
5
3
14
106
Note: Make sure than everything on the right is larger than or equal to the item and vice versa. If you get equal numbers, you can put them to the right or left, it does not matter.