Sie sind auf Seite 1von 20

Stacks

- A stack is an ordered collection of items into


which new items may be inserted and from
which items may be deleted at one end,
called the top of the stack.
- A stack can be thought of a data structure in
which 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 stack
is called popping the stack (pop).

Stacks
Stacks can be implemented as linked
lists or using array.
Array Implementation:
an integer number showing the top of the stack
an array to hold data items

Stacks
Operations:
Operation

Linked List Implementation

Array
Implementation

initialize stack

assign NULL to head

initialize top

push an item to the


stack

like 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 is
empty

if (head == NULL)

like if (top == -1)

check if stack is full

(Not applicable)

life if (top >=


STACKSIZE - 1)

Stacks
Examples on stack implementation:
Reverse a given array using a stack.
Evaluate an arithmetic expression in postfix
notation.

A*SA*M*P*L*ES*T***A*CK**
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.

Stacks Application
- Stacks evaluating arithmetic
expression.
- Suppose that one wants to find the value of a
simple arithmetic expression involving
multiplication and addition of integers, such as
5*(((9+8)*(4*6))+7).
- This expression must first be converted into
postfix notation. Customary way of writing
arithmetic expression is called infix.

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
598+46**7+*

Queues
- A queue is an ordered collection of items in which
all 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 there
is place, and removed from the front of the queue.

Queues
- Enqueue, put, or insert to add an item
- Dequeue, get, or remove to serve or
remove an item.

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.

Queues
- Queues may be implemented using linked lists or
arrays.

Queues
A*SA*M*P*LE *Q***U*EU**E*
Show how a sample queue evolves through
the series of get and put operations
represented by the sequence.

Trees
- Is a nonempty collection of vertices and
edges that satisfies certain requirements.
- One node in the tree is designated as the
root
- 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.

Trees
- Each node (except the root) has exactly one
node above it, which is called its parent.
- The nodes directly below a node are called
its children
- Nodes with no children are called leaves or
terminal nodes also called external nodes
- Nodes with children are also called
nonterminal nodes or internal nodes
- Any node is the root of a subtree consisting
of it and the nodes below it.

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 of
the children matter are called ordered trees
- The modes of a tree are divided into levels:
the level of a node is the number of nodes
on the path from the node to the root ( not
including itself).

Trees
- The height of a tree is the maximum level
among all nodes in the tree
- The path length of a tree is the sum of the
levels of all the nodes of the tree.
- the internal path length is the sum of the
levels of all the internal nodes of the tree
- The external path length is the sum of the
levels of all external nodes of the tree.

Trees
- If each node must have a specific number of
children appearing in a specific order, then we
have a multiway 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 left child and right child of
internal nodes. Each internal node must both have
a left and a right child, though one or both of them
might be an external node.

Trees
- A full binary tree is one in which the
internal nodes completely fill every level.
- The simplest way to define trees recursively
is as follows: a tree is either a single node
or 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 left
binary tree and a right binary tree.

Trees
- There is exactly one path connecting any
two 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.

Trees
- A binary tree

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.

Das könnte Ihnen auch gefallen