3 views

Original Title: 631-14

Uploaded by every9days8978

- Binary Search Tree
- Optimal Converge cast Methods for Tree- Based WSNs
- ISROTechnicalPaper2009
- Lecture 7.pdf
- Lecture-25-CS210-2012 (1).pptx
- (www.entrance-exam.net)-ISRO Sample Paper 6
- lecture14.ppt
- New
- JK
- Binary Search Trees
- Chap12 Solutions
- Data Structure in c
- Ece Oopsds Unit-4
- XCLS++: A new algorithm to improve XCLS+ for clustering XML documents
- 24Trees
- Design and Analysis of Algorithm Lab Work File
- Consistency in Database Systems
- Esc 101 Linked Lists
- Mining Interesting Rules
- Smart Forms

You are on page 1of 24

Lecture 14

A deficiency of the ADT binary tree which is corrected by the ADT binary search tree

Searching for a particular item

Record

A group of related items, called fields, that are not necessarily of the same data type

Field

A data element within a record

A data item in a binary search tree has a specially designated search key

A search key is the part of a record that identifies it within a collection of records

KeyedItem class

Contains the search key as a data field and a function for accessing the search key

4

Searches the binary search tree bintree for the item whose search key is searchKey Searches recursively in the right or left subtree (depending on the value of the item) until searchKey matches the search key of the nodes item

search(in binTree:BinarySearchTree, in searchKey:KeyType);

Inserts newItem into the binary search tree to which treePtr points

insertItem(inout treePtr:TreeNodePtr, in newItem:TreeItemType)

To copy a tree

Traverse it in preorder and insert each item visited into a new tree

Figure 10.23 (a) Insertion into an empty tree; (b) search terminates at a leaf; (c) insertion at a leaf

Exercise

Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the order given: J, N, B, A, W, E, T?

N is a leaf

Set the pointer in Ns parent to NULL

Let Ns parent adopt Ns child

N has two children

Locate another node M that is the leftmost node in Ns right subtree

Ms search key is called the inorder successor of Ns search key

Copy the item that is in M to N Remove the node M from the tree

10

Exercise

M G D A H K L U R V T W

11

Return the item with the desired search key if it exists Otherwise, throw TreeException

Traversals for a binary search tree are the same as the traversals for a binary tree

12

Theorem 10-1

The inorder traversal of a binary search tree T will visit its nodes in sorted searchkey order

Theorem 10-2

A full binary tree of height h 0 has 2h 1 nodes

Theorem 10-3

The maximum number of nodes that a binary tree of height h can have is 2h 1

13

Theorem 10-4

The minimum height of a binary tree with n nodes is log2(n+1)

The height of a particular binary search tree depends on the order in which insertion and deletion operations are performed

14

15

Applications

Treesort

Uses the ADT binary search tree to sort an array of records into search-key order

Average case: O(n * log n) Worst case: O(n2)

16

Applications

Saving a binary search tree and then restoring it to its original shape

Uses preorder traversal to save the tree to a file

Uses inorder traversal to save the tree to a file Can be used if the data is sorted and the number of nodes in the tree is known

17

binary_search

Returns true if a specified value appears in the sorted range

lower_bound; upper_bound

Returns an iterator to the first occurrence; or to one past the last occurrence of a value

equal_range

Returns a pair of iterators that indicate the first and one past the last occurrence of a value

18

General Trees

An n-ary tree

A generalization of a binary tree whose nodes each can have no more than n children

Figure 10. 41 Figure 10.38 A general tree An implementation of the n-ary tree in Figure 10.38

19

Summary

Binary trees provide a hierarchical organization of data The implementation of a binary tree is usually pointer-based A client-defined visit function to the traversal operation can customize the operations on the items of the tree

20

Summary

The binary search tree allows you to use a binary search-like algorithm to search for an item with a specified value Binary search trees come in many shapes

The height of a binary search tree with n nodes can range from a minimum of log2(n + 1) to a maximum of n The shape of a binary search tree determines the efficiency of its operations

21

Summary

An inorder traversal of a binary search tree visits the trees nodes in sorted search-key order The treesort algorithm efficiently sorts an array by using the binary search trees insertion and traversal operations

22

Summary

To restore the tree as a binary search tree of minimum height

Perform inorder traversal while saving the tree to a file

Perform preorder traversal while saving the tree to a file

23

Final Review

Binary Search Trees Trees Big-O notation Sorting Inheritance

More Topics

Linear Data Structures Manipulating Linked Lists Allocating/Deallocating memory Memory Layout and the runtime stack Exceptions Recursion

24

- Binary Search TreeUploaded bySaranya Thangaraj
- Optimal Converge cast Methods for Tree- Based WSNsUploaded byIJMER
- ISROTechnicalPaper2009Uploaded byypragati
- Lecture 7.pdfUploaded byAvanakshSingh
- Lecture-25-CS210-2012 (1).pptxUploaded byMoazzam Hussain
- (www.entrance-exam.net)-ISRO Sample Paper 6Uploaded byMradul Saxena
- lecture14.pptUploaded byAnonymous niE5VQOH
- NewUploaded byVinoth Godi
- JKUploaded byHamilton Pinheiro
- Binary Search TreesUploaded bymani271092
- Chap12 SolutionsUploaded byForm 4B
- Data Structure in cUploaded byad_prakash2010
- Ece Oopsds Unit-4Uploaded byAjay Ganesan
- XCLS++: A new algorithm to improve XCLS+ for clustering XML documentsUploaded byijitcajournal
- 24TreesUploaded bysfernando
- Design and Analysis of Algorithm Lab Work FileUploaded byRaju
- Consistency in Database SystemsUploaded bychuasharmaine
- Esc 101 Linked ListsUploaded bySheyi Adeyemi
- Mining Interesting RulesUploaded bySanethel Maan
- Smart FormsUploaded bypavelkar
- IshikawaUploaded byPradeepLokhande
- quiz section 6.docxUploaded byMarija Nikolic Jokanovic
- MCA 312 Design&Analysis of Algorithm QuestionBankUploaded bynbpr
- ASSIGNMENT 2 BY ROLL NO 44.docxUploaded bySailesh Arvapalli
- ESIPAP_MVA160208-BDTUploaded byeeeepla
- Data Structure Question Paper with Answer.pdfUploaded byMohammad Izhar Azad
- lec9-01Uploaded byHololololololo
- 10.1.1.152Uploaded byEric Laurent
- BCA-302 newUploaded byTapamalya Sanyal
- Mining Search Engine Query Logs via Suggestion SamplingUploaded bydrsharghi

- Mindshare NA's Guide to SXSW 2016Uploaded byMindshare North America
- GEP110-1104C-TAG2-LL3014BUploaded byDaniel Evans
- _htaccess Cheat Sheet - All Rules You Will Ever Need, Listed on One Page for ApacheUploaded byMichael Smith
- JosiahUploaded byShivaraju Hp
- Carbaryl a Pesticide Causes Reproductive Toxicity in Albino Rats 2161 0681.1000126Uploaded byjhika1304
- Sub-prime Crisis PptUploaded bysurajvsakpal
- business 8 syllabusUploaded byapi-242303694
- NSEP 2013 Solution 1.1Uploaded byS Prasad Shiva Pulagam
- Fluid and Electrolytes Imbalances (5)Uploaded byRy Hope
- Asian Paints - CANVAS GuidelinesUploaded bynikhildsi4481
- bankers-adda_-how-to-crack-sbi-po-2014-exam-in-2-months_.pdfUploaded bysonalibhimte
- Introduction to Yacc (Bison)Uploaded byLokesh Khandelwal
- Mathematics for IGCSE Core ExtendedUploaded bynaomi
- Hello HermanUploaded byJoe Bruno
- AN2940Uploaded byAndres Carne de Pez
- marketing chapter 3 reviewUploaded byapi-173610472
- aine mackenUploaded byapi-123092549
- Case Study of MicrosoftUploaded byRahim Ejaz
- asp-mvc.pdfUploaded byErhan Karabulut
- Formal Report 1Uploaded byAysun Güven
- Effect of Size Reduction in Complex Gold Ore ion Recovery by Aghil OjaghiUploaded byFerudun Akyol
- Schmidt Collapse of the State VectorUploaded byBen Steigmann
- GST PPT TaxguruUploaded by50raj506019
- FME OP BrochureUploaded byMartin Kratky
- Week 4Uploaded byShubham Mishra
- research proposal-1-3Uploaded byapi-252920212
- Tods 2010 Annual ReportUploaded byredali7
- Canadian Radiological Officers ManualUploaded byChuck
- Deep Photo StyleUploaded byIamIN
- Comparison of Sources HDR -- JCB-3-17925Uploaded byfofo_82