* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download data structuer Lecture 1
Survey
Document related concepts
Transcript
DATA STRUCURES II CSC 2302 QUIZ 1. What is Data Structure ? 2 . Mention the classifications of data structure giving example of each . 3 . Briefly explain the following : Stack , Queue , and an array . 4 . Mention the operations that can be performed on any linear data structure. DATA STRUCTURE • A data structure is an arrangement of data in a computer’s memory . • Arrays , Linked lists , queues , stacks , binary trees are examples of common data structures. • Different data structures works well for a diffferent purpose . • Algorithms are used to manipulate data contained in a given data structure. Classification of data structures • Data structure can be classified as either Linear data structure or Non-linear data structures • A data stucture is said to be linear if all its elements form a sequence or linear list e.g array , queue , stack and Linked list • A data structure is non-linear if its element does not form a linear list e.g Tree and Graph Data structures operations • The following operations can be perform on any linear data structure. • Traversal : Processing each element in the list • Search : Finding the location of the element with a given value • Insertion : Adding new element to the list • Deletion : Removing an element from the list Cont. • Sorting : Arranging the element in some type of order • Merging : Combining two lists into a single lists Array • An array is a list of finite number n of similar data elements referenced respectively by a set of n consecutive numbers . • Arrays can have n dimensions • One-dimentional arrays are arrays in which each element is referenced by one subscript. • A two- dimentional array is a collection of similar data elements where each element is referenced by two subscript. STACK • Some data structures allow insertion and deletion at any place in the list(beginning , middle and end) • Stack and Queue allow only insertion and deletion at the beginning or the end of the list , not in the middle. • Stack is a linear data structure in which item may be added or remove at only one end called TOP. Cont. • A handy mnemonic for stack is called LIFO • Other names of stack are ‘’piles’’ or ‘’push down list” Cont. • • • • • • Push() : Insert element in to stack POP() : Delete element from stack isEmpty() : returns true if stack has no element isFull() : Check to see if there is enough space makeEmpty () : Set stack empty Count () : returns amount of element in stack Application of stack • Used to perform efficient arithmetic operation • Use for basic designing of an OS for interrupt handling • Use to implement function call in nested procedure (eg if A is main program and B and C are sub-programs) • Use in recursion QUEUE • Is a linear list of elements • Deletion can only take place at one end called front • Insertion can take place at rear • Also called FIFO (First one enter first to go out) • In computer science example of QUEUE is in timesharing , in which program with the same priority form a QUEUE. DEQUE • A DEQUE is a linear list in which element can be added or removed at both end , but not in the middle • DEQUE refers to double ended QUEUE • Two variation of DEQUE 1. Output restricted DEQUE : allow deletion at only one end , but allow insertion at both end 2. Input restricted DEQUE : allow insertion only at one end of the list , but allow deletion at both end PRIORITY QUEUE • Is a collection of element such that each element has been asssign a priority • The following rules are applied when element are deleted and process 1) An element of higher priority is processed before any element of lower order 2) Two element with same priority are procced according to the order in which they were added to the QUEUE Cont. • Priority Queue are used in timesharing system progaram with high priority are proccesed first and progarm with same priority form a standard queue . NON-LINEAR DATA STRUCTURE ? TREES • We have disscussed only linear data structure e.g Arrays , Stack and Queue , Lists • Some other Linear data Structures not disscused are Strings and Lists • Tree is a non-linear data structure • Use mainly to represent hierachical relationship between elements e.g records , family tree and table of contents Application of tree • Organizational Chart • File System • Unix / windows File Structure Cont. • Definition : A tree is a finite set of one or more nodes . • There are diferent kinds of tree : Binary tree , 2-Tree or extended binary tree and complete binary tree Binary Tree • A binary tree T is defined as a finite set of elements , called nodes , such that : (a) T is empty (Called the null tree or empty tree). (b) T contains a distinguish node R , called Root and the remaining nodes form an ordered pair of disjoint binary trees T1 and T2 . Terminologies in Tree • Internal node : node that contains atleast one child • External node ( Leaf) : Sometimes called terminal node and does not contain any child (node) • Degree of node is the number of subtree • Edge : Is the line from node to successor Cont. • Path : Sequence od edges (Connection of two or more edges) • Level number : Each node is assign a level number , Root of tree starts with 0 always • Generation : Node with the same level number are said to be of the same generation • Depth of tree : Maximum number of node in a branch , depth can also be express as maximum number of level + 1 Cont. • Similar Trees : Binary trees are similar if they have the same structure or shape . • Tree are said to be copy if they are similar and if they have the same contents at corresponding nodes Application of Binary Tree • Some of the application of binary trees are (1) Arithmetic Expression or Expression Tree (2) Decision Tree Arithmetic Expression • Binary Trees are used to represent arithemetic expressions • Internal node : Operators • External Node : Operands Example • Consider algebraic expression E below ; E = (a-b)/((c*d)+e) This expression can be represented in binary Tree and each every algebraic expression will correspond to a unique tree and vice versa . Cont. • Each Variabe or constant in E appears as an ‘’ Internal ’’ node in T whose left and right subtrees correspond to the operands of the operation. • Below is the Corresponding Tree Complete Binary Tree A tree is said to be complete if all its level , except possibly the last , have the maximum number of possible nodes , and if all nodes at the last level appear as far left as possible . Extended Binary Trees : 2-Trees • A binary Tree T is said to be a 2-Tree or an extended binary tree if each node N has either 0 or 2 childern. • Node with 0 Childern are called external node and are represented by square • Node with 2 childern are called internal node and are represented with circle • Any binary tree can be change to extended binary tree Full Binary Tree • A full binary tree is a binary tree in which every node in the tree has 2 children except the leaves of the tree . Or • A full binary tree is a binary tree in which every node in the tree has exactly zero or 2 childern. Tree traversal • Three ways to traverse a tree (1) Preorder (2) Inorder (3) Postorder Conti (a) PreOrder : Node-Left-Right (NLF) 1 . Procces the root R 2 . Traverse the left subtree of R in preorder 3 . Traverse the right subtree of R in preorder Cont. (a) InOrder : Left-Node-Righ(LNR) 1 . Traverse the left subtree of R in preorder 2 . Procces the root R 3 . Traverse the right subtree of R in preorder Cont. (a) Post-order : Left-Node-Righ(LNR) 1 . Traverse the left subtree of R in preorder 2 . Traverse the right subtree of R in preorder 3 . Procces the root R Example