Foundations in Data Structure and Algorithms Truong Tuan Anh CSE-HCMUT Outline Basic concepts z Revision z What is Data? What is Data? z Data z z Example z z z Data is information that has been translated into a form that is more convenient to calculate and analyze Numbers, words, measurements, observations or descriptions of things Qualitative data: descriptive information Quantitative data: numerical information (numbers) z z Discrete data: can only take certain values (like whole numbers) Continuous data: can take any value (within a range) Data Type z Class of data objects that have the same properties Define a data type A set of values A set of operations on values z Example Data Structure z What is a data structure? A combination of elements in which each is either a data type or another data structure A set of associations or relationships (structure) that holds the data together z Example z An array is a number of elements of the same type in a specific order Abstract Data Type z Aim z z z Users know what a data type can How it is done is hidden Definition An abstract data type is a data declaration packaged together with the operations that are meaningful for the data type Declaration of data Declaration of operations Encapsulation of data and operations Abstract Data Type Example: List z Interface Data: sequence of elements of a particular data type z Operations: accessing, insertion, deletion z z Implementation z z Array Linked list Algorithm What is an algorithm? The logical steps to solve a problem What is a program? Program = Data structures + Algorithms (Niklaus Wirth) 10 Breadth-First Traversals z In the breadth-first traversal of a binary tree, we process all of the children of a node before proceeding with the next level 21 Binary Search Trees 22 Binary Search Trees z A binary search tree is a binary tree with the following properties: All items in the left subtree are less than the root All items in the right subtree are greater than or equal to the root Each subtree is itself a binary search tree 23 Valid Binary Search Trees 24 Invalid Binary Search Trees 25 Binary Search Trees (BST) z z z BST is one of implementations for ordered list In BST we can search quickly (as with binary search on a contiguous list) In BST we can make insertions and deletions quickly (as with a linked list) 26 Binary Search Trees: Traversals 27 Operations 28 Insert a Node into BST z All BST insertions take place at a leaf or a leaflike node (a node that has only one null branch) 29 Delete a Node from BST z Deletion of a leaf: Set the deleted node's parent link to NULL 30 Delete a Node from BST z Deletion of a node having only right subtree or left subtree: Attach the subtree to the deleted node's parent 31 Delete a Node from BST z Deletion of a node having both subtrees: z Replace the deleted node by its predecessor or by its successor, recycle this node instead 32 Delete a Node from BST 33 Delete a Node from BST 34 Takeaways Basic concepts z Trees z 35 ... Statement constructs: sequence, selection, iteration 13 Pseudocode: Example 14 Data Structures 15 Data Structures 16 Data Structures A member of an object can be accessed directly by a dot (.)