Sessions 26 and 27 - Data Structures & Algorithm

2 165 827 33 652 908 11 44 895 925 preorder traversal

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ta Structures & Algorithm 72 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 cout << "\nPreorder traversal\n"; intTree.preOrderTraversal(); cout << "\nInorder traversal\n"; intTree.inOrderTraversal(); cout << "\nPostorder traversal\n"; intTree.postOrderTraversal(); Tree< double > doubleTree; // create Tree of double values double doubleValue; cout << fixed << setprecision( 1 ) << "\n\n\nEnter 10 double values:\n"; for ( int j = 0; j < 10; j++ ) { cin >> doubleValue; doubleTree.insertNode( doubleValue ); } // end for cout << "\nPreorder traversal\n"; doubleTree.preOrderTraversal(); Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 73 51 52 53 54 55 56 57 58 58 59 60 61 cout << "\nInorder traversal\n"; doubleTree.inOrderTraversal(); cout << "\nPostorder traversal\n"; doubleTree.postOrderTraversal(); cout << endl; return 0; } // end main Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 74 Enter 10 integer values: 50 25 75 12 33 67 88 6 13 68 Preorder traversal 50 25 12 6 13 33 75 67 68 88 Inorder traversal 6 12 13 25 33 50 67 68 75 88 Postorder traversal 6 13 12 33 25 68 67 88 75 50 Enter 10 double values: 39.2 16.5 82.7 3.3 65.2 90.8 1.1 4.4 89.5 92.5 Preorder traversal 39.2 16.5 3.3 1.1 4.4 82.7 65.2 90.8 89.5 92.5 Inorder traversal 1.1 3.3 4.4 16.5 39.2 65.2 82.7 89.5 90.8 92.5 Postorder traversal 1.1 4.4 3.3 16.5 65.2 89.5 92.5 90.8 82.7 39.2 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 75 SORT AND SEARCH ALGORITHMS Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 76 Searching Algorithms Searching large amounts of data Looking up a phone number Accessing a Web site Checking the definition of a word in a dictionary all involve Goalfinding an element that matches a given search key Amount of effort they require to complete the search Big O notation For searching and sorting algorithms, this is particularly dependent on the number of data elements Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 77 Big O notation Suppose an algorithm simply tests whether the first element of a vector is equal to the second element of the vector If the vector has 10 elements, this algorithm requires one comparison. If the vector has 1000 elements, the algorithm still requires one comparison. The algorithm is completely independent of the number of elements in the vector. This algorithm is said to have a constant runtime which is represented in Big O notation as O(1). O(1) just means that the number of comparisons is constant It does not grow as the size of the vector increases Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 78 Big O notation An algorithm that tests whether the first element of a vector is equal to any of the other elements of the vector requires at most n - 1 comparisons, where n is the number of elements in the v...
View Full Document

Ask a homework question - tutors are online