Download presentation
Presentation is loading. Please wait.
Published byMeredith Foster Modified over 9 years ago
3
Trees Linear access time of linked lists is prohibitive Does there exist any simple data structure for which the running time of most operations (search, insert, delete) is O(log N)? Trees Basic concepts Tree traversal Binary tree Binary search tree and its operations
4
Trees A tree is a collection of nodes The collection can be empty (recursive definition) If not empty, a tree consists of a distinguished node r (the root), and zero or more nonempty subtrees T 1, T 2,...., T k, each of whose roots are connected by a directed edge from r
5
Some Terminologies Child and Parent Every node except the root has one parent A node can have an zero or more children Leaves Leaves are nodes with no children Sibling nodes with same parent
6
More Terminologies Path A sequence of edges Length of a path number of edges on the path Depth of a node length of the unique path from the root to that node Height of a node length of the longest path from that node to a leaf all leaves are at height 0 The height of a tree = the height of the root = the depth of the deepest leaf Ancestor and descendant If there is a path from n1 to n2 n1 is an ancestor of n2, n2 is a descendant of n1 Proper ancestor and proper descendant
7
Example: UNIX Directory
8
Example: Expression Trees Leaves are operands (constants or variables) The internal nodes contain operators Will not be a binary tree if some operators are not binary
9
Tree Traversal Used to print out the data in a tree in a certain order Pre-order traversal Print the data at the root Recursively print out all data in the left subtree Recursively print out all data in the right subtree
10
Preorder, Postorder and Inorder Preorder traversal node, left, right prefix expression ○ ++a*bc*+*defg
11
Preorder, Postorder and Inorder Postorder traversal left, right, node postfix expression ○ abc*+de*f+g*+ Inorder traversal left, node, right infix expression ○ a+b*c+d*e+f*g
12
Example: Unix Directory Traversal PreOrder PostOrder
13
Preorder, Postorder and Inorder Pseudo Code
14
Binary Trees A tree in which no node can have more than two children The depth of an “average” binary tree is considerably smaller than N, even though in the worst case, the depth can be as large as N – 1. Generic binary tree Worst-case binary tree
15
Node Struct of Binary Tree Possible operations on the Binary Tree ADT Parent, left_child, right_child, sibling, root, etc Implementation Because a binary tree has at most two children, we can keep direct pointers to them
16
Convert a Generic Tree to a Binary Tree
17
Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X
18
Binary Search Trees A binary search tree Not a binary search tree
19
Binary Search Trees Average depth of a node is O(log N) Maximum depth of a node is O(N) The same set of keys may have different BSTs
20
Searching BST If we are searching for 15, then we are done. If we are searching for a key < 15, then we should search in the left subtree. If we are searching for a key > 15, then we should search in the right subtree.
22
Searching Find X: return a pointer to the node that has key X, or NULL if there is no such node Time complexity: O(height of the tree) BinaryNode * BinarySearchTree::Find(const float &x, BinaryNode *t) const { if (t == NULL) return NULL; else if (x element) return Find(x, t->left); else if (t->element < x) return Find(x, t->right); else return t; // match }
23
In-order Traversal of BST Inorder traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
24
findMin/ findMax Goal: return the node containing the smallest (largest) key in the tree Algorithm: Start at the root and go left (right) as long as there is a left (right) child. The stopping point is the smallest (largest) element Time complexity = O(height of the tree) BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const { if (t == NULL) return NULL; if (t->left == NULL) return t; return FindMin(t->left); }
25
Insertion Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path traversed Time complexity = O(height of the tree)
26
Deletion When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.
27
Deletion under Different Cases Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node
28
Deletion Case 3 Case 3: the node has 2 children Replace the key of that node with the minimum element at the right subtree Delete that minimum element ○ Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. Time complexity = O(height of the tree)
29
l 29 AVL Trees Keep the tree balanced Use node rotation Balanced condition: The left and the right subtrees of each node should differ by at most one level. The method was proposed by two Russian scientists Adelson-Velskii and Landis in 1962
30
l 30 Single Rotation l5l5 l3l3 l7l7 l6l6 l8l8 l9l9 l The sub-tree containing the inserted node (rooted at 8) is at the same side as the ‘heavier’ sub-tree of node 5 (rooted at 7) l Links to be changed l New node
31
l 31 After the Rotation l7l7 l5l5 l8l8 l9l9 l6l6 l3l3 l The middle node 6 is switched to the other subtree
32
l 32 Double Rotation l5l5 l6l6 l7l7 l8l8 l5l5 l7l7 l8l8 l9l9 l6l6 l9l9 l8l8 l8l8 l7l7 l7l7 l The new node 6 is in the left subtree of node 8, while node 8 is the right subtree of 5 -> rotate 7 and 8 to obtain same-side trees. l New node
33
l 33 Splay Trees l Move to the top each accessed node with rotations, decreasing the depth of the tree.
34
l 34 Multi-Way Search l Nodes contain more than one key l nodes are used only to contain the keys, the actual records are stored at the terminal nodes at the bottom of the tree
35
l 35 Complexity Issues AVL trees: Search,Insertion, and Deletion: O(logN) Creating the tree – O(N) Trees with multiple keys: O(Log m N) m – branching factor
37
Linear search In computer science, linear search or sequential search is a method for finding a particular value in a list, that consists of checking every one of its elements, one at a time and in sequence, until the desired one is found.computer sciencelist Linear search is the simplest search algorithm.search algorithm
38
Linear search (continued) The simplest type of searching process. In the sequential search, each element of the array is compared to the key, in the order it appears in the array, until the first element matching the key is found.
39
Example Algorithm LINEAR vs. BINARY Finding the oldest person in a room full of people 1. Understanding the problem initial condition – room full of people goal – identify the oldest person assumptions a person will give their real birthday if two people are born on the same day, they are the same age if there is more than one oldest person, finding any one of them is okay
40
Finding the oldest person (algorithm 1) 1. line up all the people along one wall 2. ask the first person to state his or her name and birthday, then write this information down on a piece of paper 3. for each successive person in line: i. ask the person for his or her name and birthday ii. if the stated birthday is earlier than the birthday on the paper, cross out old information and write down the name and birthday of this person Note: When you reach the end of the line, the name and birthday of the oldest person will be written on the paper
41
l Finding the oldest person (algorithm 2) 1. line up all the people along one wall 2. as long as there is more than one person in the line, repeatedly i. have the people pair up (1 st with 2 nd, 3 rd with 4 th, etc) – if there is an odd number of people, the last person will be without a partner ii. ask each pair of people to compare their birthdays iii. request that the younger of the two leave the line when there is only one person left in line, that person is the oldest
42
for algorithm 1: ○ 100 people 5*100 = 500 seconds ○ 200 people 5*200 = 1000 seconds ○ 400 people 5*400 = 2000 seconds... ○ 1,000,000 people 5*1,000,000 = 5,000,000 seconds for algorithm 2: ○ 100 people 5* log 2 100 = 35 seconds ○ 200 people 5* log 2 200 = 40 seconds ○ 400 people 5* log 2 400 = 45 seconds... ○ 1,000,000 people 5* log 2 1,000,000 = 100 seconds
43
Analysis For a list with n items, the best case is when the value is equal to the first element of the list, in which case only one comparison is needed. The worst case is when the value is not in the list (or occurs only once at the end of the list), in which case n comparisons are needed. If the value being sought occurs k times in the list, and all orderings of the list are equally likely, the expected number of comparisons is: Asymptotically, therefore, the worst-case cost and the expected cost of linear search are both O(n). AsymptoticallyO
44
APPLICATION Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list. When many values have to be searched in the same list, it often pays to pre-process the latter in order to use a faster method. For example, one may sort the list and use binary search, or build any efficient search data structure from it.sortsearch data structure
45
ALGORITHM The following ALGORITHM describes a typical variant of linear search, where the result of the search is supposed to be either the location of the list item where the desired value was found; or an invalid location Λ, to indicate that the desired element does not occur in the list. Note: the last line is executed only after all list items have been examined with none matching.
46
If the list is stored as an array data structure, the location may be the index of the item found (usually between 1 and n, or 0 and n−1). In that case the invalid location Λ can be any index before the first element (such as 0 or −1, respectively) or after the last one (n+1 or n, respectively).array data structureindex
47
Recursive version Linear search can also be described as a recursive algorithm:recursive algorithm
48
Searching in reverse order Linear search in an array that is usually programmed by stepping up an index variable until it reaches the last index. In many computers, one can reduce the work of the first comparison by scanning the items in reverse order.
49
Example Algorithm Suppose an array A with elements indexed 1 to n is to be searched for a value x. The following pseudocode performs a forward search, returning n + 1 if the value is not found:
50
The following pseudocode searches the array in the reverse order, returning 0 when the element is not found:
51
End… Thank you
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.