stacks queues trees
TRANSCRIPT
-
8/17/2019 Stacks Queues Trees
1/20
Stacks
- A stack is an ordered collection of items intowhich new items may be inserted and fromwhich items may be deleted at one end,called the top of the stack.
- A stack can be thought of a data structure inwhich only the top element can be accessed.
- A Stack is a LIFO (Last In First Out) structure.
The item that is put last to the stack, is taken first.- Storing an item in a stack is called pushing it onto
the stack (push). Removing a value from a stackis called popping the stack (pop).
-
8/17/2019 Stacks Queues Trees
2/20
Stacks
• Stacks can be implemented as linkedlists or using array.
• Array Implementation:
– an integer number showing the top of the stack
– an array to hold data items
-
8/17/2019 Stacks Queues Trees
3/20
Stacks
Operations:
Operation Linked List ImplementationArray
Implementation
initialize stack assign NULL to head initialize top
push an item to the
stacklike add_begin
increment top and
store data
pop an item from
the stack
similar to delete_begin but
returns data
get data and
decrement top
check if stack isempty if (head == NULL) like if (top == -1)
check if stack is full (Not applicable)life if (top >=
STACKSIZE - 1)
-
8/17/2019 Stacks Queues Trees
4/20
Stacks
• Examples on stack implementation: – Reverse a given array using a stack.
– Evaluate an arithmetic expression in postfix
notation.
A * S A * M * P * L * E S * T * * * A * C K * *
Show the dynamic characteristics of a stack by tracing the
stack in every movement. Each letter in the list means
“push (the letter)”; each asterisk mean “pop.
-
8/17/2019 Stacks Queues Trees
5/20
Stacks Application
- Stacks evaluating arithmeticexpression.- Suppose that one wants to find the value of a
simple arithmetic expression involvingmultiplication and addition of integers, such as
5*(((9+8)*(4*6))+7).
- This expression must first be converted intopost f ix notation. Customary way of writing
arithmetic expression is called inf ix .
-
8/17/2019 Stacks Queues Trees
6/20
Stacks Application (continuation..)
Algorithm
- Convert into postfix notation.- Disregard all left parenthesis.
- Scan the expression from left to right. Output an
operand if found. If an Operator is found push to
the stack. If a right parenthesis is found,
perform a pop and output to the operand.- Following the algorithm we have
5 9 8 + 4 6 * * 7 + *
-
8/17/2019 Stacks Queues Trees
7/20
Queues
- A queue is an ordered collection of items in whichall insertions take place at one end and all
deletions take place at the opposite end. The first
element inserted into a queue is the first element
removed.
- A queue is a FIFO (First In First Out) structure.
The item that is put first to the queue, is taken first.
- Items are inserted at the rear of the queue if thereis place, and removed from the front of the queue.
-
8/17/2019 Stacks Queues Trees
8/20
Queues
- “Enqueue, pu t, o r insert ” to add an item
- “Dequeue, get, o r remove ” to serve or
remove an item.
-
8/17/2019 Stacks Queues Trees
9/20
Queues
• Operations: – initialize a queue
– insert an item to the queue
– remove an item from the queue – check if queue is empty
– check if queue is full
• Examples of queues:
– Queues in a restaurant.
– Queues in a bank.
– Queues in a theater.
-
8/17/2019 Stacks Queues Trees
10/20
Queues
- Queues may be implemented using linked lists orarrays.
-
8/17/2019 Stacks Queues Trees
11/20
Queues
A * S A * M * P * L E * Q * * * U * E U * * E *
Show how a sample queue evolves throughthe series of get and put operations
represented by the sequence.
-
8/17/2019 Stacks Queues Trees
12/20
Trees
- Is a nonempty collection of vert ices andedges that satisfies certain requirements.
- One node in the tree is designated as the
roo t
- A path in a tree is a list of distinct vertices in
which successive vertices are connected by
edges in the tree.- There is exactly one path between the root
and each of the other nodes in the tree.
-
8/17/2019 Stacks Queues Trees
13/20
Trees
- Each node (except the root) has exactly onenode above it, which is called its parent.
- The nodes directly below a node are called
its chi ldren - Nodes with no children are called leaves orterminal nodes also called external nodes
- Nodes with children are also callednonterminal nodes or internal nodes
- Any node is the root of a subtree consistingof it and the nodes below it.
-
8/17/2019 Stacks Queues Trees
14/20
Trees
- A set of tress is called a forest- Sometimes the way in which the children of
each node are ordered is significant,
sometimes not. Trees wherein the order ofthe children matter are called ordered trees
- The modes of a tree are divided into levels:
the level of a node is the number of nodeson the path from the node to the root ( not
including itself).
-
8/17/2019 Stacks Queues Trees
15/20
Trees
- The height of a tree is the maximum levelamong all nodes in the tree
- The path leng th of a tree is the sum of the
levels of all the nodes of the tree.- the internal path leng th is the sum of the
levels of all the internal nodes of the tree
- The external path leng th is the sum of thelevels of all external nodes of the tree.
-
8/17/2019 Stacks Queues Trees
16/20
Trees
- If each node must have a specific number ofchildren appearing in a specific order, then we
have a mult iway tree.
- A binary tree is a multiway tree consisting of two
types of nodes: external nodes with no children
and internal nodes with exactly two children. Since
the two children of each internal node are ordered,
we refer to the lef t ch i ld and r ight ch i ld ofinternal nodes. Each internal node must both have
a left and a right child, though one or both of them
might be an external node.
-
8/17/2019 Stacks Queues Trees
17/20
Trees
- A fu l l b inary tree is one in which theinternal nodes completely fill every level.
- The simplest way to define trees recursively
is as follows: “ a tree is either a single nodeor a root node connected to a set of trees”
and “a binary tree is either an external node
or a root (internal) node connected to a leftbinary tree and a right binary tree.”
-
8/17/2019 Stacks Queues Trees
18/20
Trees
- There is exactly one path connecting anytwo nodes in a tree
- A tree with N nodes has N-1 edges
- A binary tree with N internal nodes has N+1 external nodes
- the height of a full binary tree with N internal
nodes is about log2N.
-
8/17/2019 Stacks Queues Trees
19/20
Trees
- A binary tree
-
8/17/2019 Stacks Queues Trees
20/20
Trees
- Tree traversals- Preorder: Visit the root, then the left subtree,
then the right subtree.
- Inorder : Visit the left subtree, then the root,then the right subtree.
- Postorder : Visit the left subtree, then the right
subtree, then the root.
- Level-order : Visit the nodes from left to right
level by level.