1 / 30

Algorithms and Data Structures الخوارزميات و تراكيب البيانات CS 211 Lecture 5: Stack Queue Tree

Algorithms and Data Structures الخوارزميات و تراكيب البيانات CS 211 Lecture 5: Stack Queue Tree Graph. What is Stack ?. Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be :

craigb
Download Presentation

Algorithms and Data Structures الخوارزميات و تراكيب البيانات CS 211 Lecture 5: Stack Queue Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and Data Structuresالخوارزميات و تراكيب البياناتCS 211 Lecture 5: Stack Queue Tree Graph

  2. What is Stack ? • Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be : LIFO(Last In First Out) or FILO(First In Last Out).

  3. Stack Mainly the following three basic operations are performed in the stack: • Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow condition. • Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. • Peek or Top: Returns top element of stack.

  4. 1 6 6 7 1 1 7 6 6 Push operation Top Top X Top X X Top

  5. 6 1 7 6 1 7 6 1 Pop operation X Top Top Top

  6. 6 1 7 6 1 7 Peek operation : Top.name Top.salary … Top How to empty the Stack ? X Top

  7. Queue

  8. What isQueue ? • A Queue is a linear data structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO). • The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added. Rear Front Hussam Ahmed Ali Mohamed hussain

  9. Queue The principal operations : • Addition of entities to the rear position, known as enqueue, • Removal of entities from the front position, known as dequeue. • Returning the value of the front element without dequeuing it, known as peek.

  10. Queue

  11. EnQueue operation Rear Front 2 X New item Ahmed Ali Mohamed Hussain 1 DeQueue operation Rear Front X Ali Mohamed Hussain Ahmad

  12. Peek Operation front.name front.salary … How to empty the Queue ? Rear Front X Ali Mohamed Hussain Ahmad

  13. Tree & Graph

  14. Tree • A tree is a finite collection of data items. It is a non-linear data structure . • It is used to show a hierarchical structure between the data elements and organizes the data into branches which relate the information. • There are several types of trees such as a binary tree, binary search tree, AVL tree, threaded binary tree, B-tree, etc…

  15. Tree

  16. Tree root node جذر internal nodes عناصرداخلية leaf nodes الأوراق

  17. Tree

  18. Edge – A line which connects two nodes. • Level – A tree is partitioned into levels. The root node is at level 0. Then, its immediate children are at level 1, and its immediate children are at level 2 and so on up to the terminal or leaf node. • Degree – It is the number of subtrees of a node in a given tree. • Depth – It is the maximum level of any node in a given tree and also known as height. • Terminal node – The highest level node is terminal node while other nodes except terminal and root node are known as non-terminal nodes.

  19. properties of tree • A tree must be connected which means there must be a path from the root to all other nodes. • It does not contain any loops. • If n= number of nodes, a tree has n-1 edges.

  20. B A D C Binary Tree • It is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.

  21. Binary Search Tree It is a binary tree data structure which has the following properties: • The left subtree of a node contains only nodes with keys lesser than the node’s key. • The right subtree of a node contains only nodes with keys greater than the node’s key. • The left and right subtree each must also be a binary search tree.

  22. Binary Search Tree 5 10 10 2 45 5 30 5 45 30 2 25 45 2 25 30 10 25 Not a binary search tree Binary search trees

  23. Graph • Agraph is non-linear data structure. It consists of a group of vertices (or nodes) and set of edges that connect two vertices. • Vertices on the graph is represented as point or circles and edges are shown as arcs or line segments. • An edge is represented by E(v,w) where v and w are the pairs of vertices. • The graphs are classified into various categories such as directed, non-directed, connected, non-connected, simple and multi-graph.

  24. Directed graph (digraph(

  25. UnDirected graph

  26. Properties of a graph • In a graph, a vertex can be connected to any number of other vertices using edges. • An edge can be bi-directed or directed. • An edge can be weighted.

  27. Adjacent vertices – A vertex a is adjacent to vertex b if there is an edge (a,b) or (b,a). • Path – A path from a random vertex w is an adjacent sequence of vertices. • Cycle – It is a path where the first and last vertices are the same. • Degree – It is a number of edges incident on a vertex. • Connected graph – If there exists a path from a random vertex to any other vertex, then that graph is known as a connected graph.

  28. Differences Between Tree and Graph • In a tree, there exist only one path between any two vertices whereas a graph can have unidirectional and bidirectional paths between the nodes. • In the tree, there is exactly one root node, and every child can have only one parent. As against, in a graph, there is no concept of the root node. • A tree can not have loops while graph can have loops. • A tree can have n-1 edges. On the contrary, in the graph, there is no predefined number of edges. (n=number of nodes). • A tree has a hierarchical structure whereas graph has a network model.

  29. COMPARISONBetween Tree and Graph

More Related