Queue Stack Graph Tree(3)
Queue Stack Graph Tree(3)
(Prepared by)
Dr. Thinn Thu Naing
Rector
University of Computer Studies, Taunggyi
Queue
Queue
Definition Methods of Queue
A queue is a data structure used to • Enqueue: places an item at the
model a First-In-First-Out (FIFO) back of the queue;
strategy. Conceptually, we add to • Dequeue: retrieves the item at
the end of a queue and take away the front of the queue, and
elements from its front. removes it from the queue;
• Peek: retrieves the item at the
front of the queue without
Types of Queue
removing it from the queue
• A standard queue
• Priority Queue
• Double Ended Queue
Standard Queue
• The main property of a queue is that we have access to the item at the front of
the queue.
• The queue data structure can be efficiently implemented using a singly linked list
• A singly linked list provides O(1) insertion and deletion run time complexities.
• The reason we have an O(1) run time complexity for deletion is because we only
ever remove items from the front of queues (with the Dequeue operation).
Applications of Queue
•CPU Scheduling in Processor
•Disk Scheduling in Secondary
Storage
•Job scheduling algorithms.
•Round robin scheduling.
•Recognizing a palindrome.
Priority Queue
• a priority queue determines the order of its items by using a form of custom comparer
to see which item has the highest priority.
• the items in a priority queue being ordered by priority it remains the same as a normal
queue: It can only access the item at the front of the queue.
• a priority queue is to use a heap data structure (Heap tree).
– bin packing
– linear programming,
• Backtracking (Searching)
– game playing
– finding paths
– exhaustive searching
Definition
• A Graph is a non-linear data structure
consisting of nodes and edges. The
nodes are sometimes also referred to as
vertices and the edges are lines or arcs
that connect any two nodes in the
graph.
the Graph in figure ,
• A Graph consists of a finite set of
the set of vertices V = {0,1,2,3,4} and
vertices(or nodes) and set of Edges
the set of edges E = {01, 12, 23, 34, 04, 14, 13}.
which connect a pair of nodes.
Topological Map of
Railway Station
Tree
Definition Tree’s terminology
A tree as consisting of nodes (also called vertices Root- a unique ‘top level’ node
or points) and edges (also called lines, or, in Child node - is connected to the
order to stress the directedness, arcs) with a given node via a branch
tree-like structure.
Parent node – is connected to the
given node (via an edge) on the level
above
Sibling : Nodes that have the same
parent are known as siblings
The depth or level of a node is given
by the length of this path.
The maximal length of a path in a
tree is also called the height of the
tree.
The size of a tree is given by the
Types of Tree number of nodes it contains.
• Quad Tree
• Binary Tree
• Binary Search Tree
• AVL Tree
• Heap Trees
Quad-trees
Preorder Traversal
The preorder algorithm, you visit the root first, then traverse the left
subtree and finally traverse the right subtree. (Root, Left, Right)
Traversal (Binary Search Tree)
Postorder Traversal
The postorder algorithm, you visit the left subtree and traverse the right
subtree, finally traverse the root. (Left, Right, Root)
Traversal (Binary Search Tree)
Inorder Traversal
The inorder algorithm, you visit the left subtree first, then traverse the
root and finally traverse the right subtree. (Left, Roof, Right)
AVL Tree
Fig (1) binary search tree represents the worst case scenario in which the running time
of all common operations such as search, insertion and deletion are O(n).
By applying a balance condition (Fig (2) , we ensure that the worst case running time
of each common operation is O(log n).
A heap tree is a complete binary tree which is either empty or satisfies the
following conditions:
• The priority of the root is higher than (or equal to) that of its children.
• The left and right subtrees of the root are heap trees.
We can start by simply drawing the array as a tree, and see that the last 5 entries
(those with indices greater than 9/2 = 4) are leaves of the tree, as follows:
i= 1
i= 2 i= 3
i= 4 i= 5 i= 6 i= 7
i= 8 i= 9