# Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,

## Presentation on theme: "Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,"— Presentation transcript:

unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort, mergesort basic programming concepts object oriented programming topics in computer science syllabus

unit 11a 2 Graph: a data representation which includes nodes and edges, where each edge connects two nodes Example: the internet Tree: a connected graph with no loops, and a root Example: inheritance tree Graphs and Trees

unit 11a 3 Graphs and Trees a) b) c) ROOT LEAF NODES internal vertices

unit 11a 4 Binary Trees H A rooted tree is called a binary tree if every internal vertex has no more than 2 children The tree is called a full binary tree if every internal vertex has exactly 2 children H An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered; we call the children of a vertex the left child and the right child, if they exist H A rooted binary tree of height H is called balanced if all its leaves are at levels H or H-1

unit 11a 5 Binary tree: example Hal Lou Ken Joe Ted Sue Ed Max

unit 11a 6 Theorem: A tree with N vertices has N-1 edges Theorem: There are at most 2 H leaves in a binary tree of height H Corallary: If a binary tree with L leaves is full and balanced, then its height is H =  log 2 L  Theorem: There are at most (2 H+1 –1) nodes in a binary tree of height H Tree Properties

unit 11a A special kind of binary tree in which: 1. Each vertex contains a distinct key value 2. The key values in the tree can be compared using “greater than” and “less than” 3. The key value of each vertex in the tree is less than every key value in its left subtree, and greater than every key value in its right subtree Binary Search Tree (BST)

unit 11a 8 Example: Binary Search Tree Hal Lou Ken Joe Ted Sue Ed Max

unit 11a 9 Shape of a BST Depends on its key values and their order of insertion: H Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order. H The first value to be inserted is put into the root. ‘J’

unit 11a 10 Inserting ‘E’ into the BST Thereafter, each value to be inserted begins by comparing itself to the value in the root, moving left it is less, or moving right if it is greater. This continues at each level until it can be inserted as a new leaf. ‘J’ ‘E’

unit 11a 11 Begin by comparing ‘F’ to the value in the root, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘F’ into the BST ‘J’ ‘E’ ‘F’

unit 11a 12 Begin by comparing ‘T’ to the value in the root, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘T’ into the BST ‘J’ ‘E’ ‘F’ ‘T’

unit 11a 13 Begin by comparing ‘A’ to the value in the root, moving left it is less, or moving right if it is greater. This continues until it can be inserted as a leaf. Inserting ‘A’ into the BST ‘J’ ‘E’ ‘F’ ‘T’ ‘A’

unit 11a 14 what BST is obtained by inserting the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order? Order of insertion ‘A’ ‘E’ ‘F’ ‘J’ ‘T’

unit 11a 15 Another binary search tree Add nodes containing these values in this order: ‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’ ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘P’

unit 11a 16 Task: is ‘F’ in the tree? ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’ ‘K’ ‘V’ ‘P’ ‘Z’‘D’‘Q’‘L’‘B’‘S’

unit 11a 17 Search(x) H start at the root of the tree which contains y: 1. the tree is empty  x is not present 2. x = y (the item at the root)  the root is returned 3. x < y  recursively search the left subtree 4. x > y  recursively search the right subtree

unit 11a 18 Operations H Search(x) H Insert(x) H Delete(x) tree algs/demo

unit 11a 19 Complexity H Search(x) – O(H) H Insert(x) – O(H) H Delete(x) – O(H)  worst case O(n) when tree is a list best case O(log n) when tree is full and balanced

unit 11a 20 H A traversal algorithm is a procedure for systematically visiting every vertex of an ordered binary tree H Tree traversals are defined recursively H Three traversals are named: preorder inorder postorder Traversal Algorithms

unit 11a 21 Let T be an ordered binary tree with root r: H If T has only r, then r is the preorder traversal H Otherwise, suppose T 1, T 2 are the left and right subtrees at r; the preorder traversal is visit r traverse T 1 in preorder traverse T 2 in preorder PREORDER Traversal Algorithm

unit 11a 22 Preorder Traversal ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ ROOT Visit left subtree secondVisit right subtree last Visit first result: J E A H T M Y

unit 11a 23 Let T be an ordered binary tree with root r: H If T has only r, then r is the inorder traversal H Otherwise, suppose T 1, T 2 are the left and right subtrees at r; then traverse T 1 in inorder visit r traverse T 2 in inorder INORDER Traversal Algorithm

unit 11a 24 Inorder Traversal ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ ROOT Visit left subtree firstVisit right subtree last Visit second result: A E H J M T Y

unit 11a 25 Let T be an ordered binary tree with root r: H If T has only r, then r is the postorder traversal H Otherwise, suppose T 1, T 2 are the left and right subtrees at r; then traverse T 1 in postorder traverse T 2 in postorder visit r POSTORDER Traversal Algorithm

unit 11a 26 ‘J’ ‘E’ ‘A’ ‘H’ ‘T’ ‘M’‘Y’ ROOT Visit left subtree firstVisit right subtree second Visit last Postorder Traversal result: A H E M Y T J

unit 11a 27 A Binary Expression Tree ‘-’ ‘8’ ‘5’ ROOT INORDER TRAVERSAL : 8 - 5 has value 3 PREORDER TRAVERSAL: - 8 5 POSTORDER TRAVERSAL: 8 5 -

unit 11a 28 A special kind of binary tree in which: 1. Each leaf node contains a single operand 2. Each nonleaf node contains a single binary operator 3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree Binary Expression Tree

unit 11a 29 A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ What value does it have? ( 4 + 2 ) * 3 = 18

unit 11a 30 A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ What infix, prefix, postfix expressions does it represent?

unit 11a 31 A Binary Expression Tree ‘*’ ‘+’ ‘4’ ‘3’ ‘2’ Infix: ( ( 4 + 2 ) * 3 ) Prefix: * + 4 2 3 evaluate from right Postfix: 4 2 + 3 * evaluate from left

unit 11a 32 Levels Indicate Precedence When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation Operations at higher levels of the tree are evaluated later than those below them; the operation at the root is always the last operation performed

unit 11a 33 Example ‘*’ ‘-’ ‘8’ ‘5’ What infix, prefix, postfix expressions does it represent? ‘/’ ‘+’ ‘4’ ‘3’ ‘2’

unit 11a 34 A binary expression tree Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) ) Prefix: * - 8 5 / + 4 2 3 Postfix: 8 5 - 4 2 + 3 / * has operators in order used ‘*’ ‘-’ ‘8’ ‘5’ ‘/’ ‘+’ ‘4’ ‘3’ ‘2’

unit 11a 35 Hash tables H Goal: accesses data with complexity O(1), even when the data needs to be dynamically administered (mostly by inserting new or deleting existing data) H Solution: array H Problem: access is only O(1) if the index of the element is known – otherwise O(log n) H Solution: each data item to be stored is associated with a hash value which gives array index: generate array of size m, where m is prime and sufficiently large assign unique numerical value N(k) to key of element k h(k) = N(k) mod m

unit 11a 36 Hash tables - cont H Problem: hash value is not unique anymore! H Solution: a collision procedure must determine the position for the new object hasshing

unit 11a 37 Operations H Search(x) H Insert(x) H Delete(x) Complexity:  worst case O(n) when hash value is always the same (and table is really a list)  best case O(1) when hash value is always distinct

unit 11a 38 Example: keys are names H list of unique identifiers washington  103288042987600 lincoln  5201793578 bush  151444 H the range currently is all positive integers, which is not possible to implement H for m=10007 washington  3249 lincoln  4873 bush  1339

unit 11a 39 why modulus prime number? H underlying reason: the new code numbers should appear random H technical reason: the modulus operation should be a “field” H example: original codes are all even numbers m is even only even buckets in the table will get filled, half the table will be empty

unit 11a 40 Back to sorting... Two recursive algorithms: H MergeSort H QuickSort

unit 11a 41 MergeSort H divide array to two equal parts H recursively sort left part H recursively sort right part H merge the two sorted lists MergeSort

unit 11a 42 QuickSort quicksort H choose pivot arbitrarily H divide to left part ( than pivot) H sort each part recursively

unit 11a 43 Complexity H MergeSort worst case O(n * log n) H QuickSort worst case O(n 2 ) average case O(n * log n)

Download ppt "Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,"

Similar presentations