11 views

Uploaded by Rahul Upamanyu

A very easy and good explanation of binary search tree in data structures.

save

You are on page 1of 2

**Suppose T is a binary tree. Then, T is called a binary search tree (or binary sorted tree) if
**

each node N of T has the following property:

The value at N is greater than every value in the left subtree of N and is less than every

value in the right subtree of N.

Searching and Inserting in Binary search tree

**The searching and inserting will be given by a single search and insertion algorithm.
**

Suppose an ITEM of information is given. The following algorithm finds the location of ITEM

in the binary search tree T, or inserts ITEM as a new node in its appropriate place in the

tree.

a) Compare ITEM with the root node N of the tree.

(i) If ITEM < N, proceed to the left child of N.

(ii) If ITEM > N, proceed to the right child of N.

b) Repeat step (a) until one of the following occurs:

(i) We meet a node N such that ITEM = N. in this case the search is successful.

(ii) We meet an empty subtree, which indicate that the search is unsuccessful, and we insert

ITEM in place of the empty subtree.

In other words, proceed from the root R down through the tree T until finding ITEM in T or

inserting ITEM as a terminal node in T.

**The formal presentation of our search and insertion algorithm will use the following
**

procedure, which finds the location of a given ITEM and its parent. The procedure traverses

down the tree using the pointer PTR and the pointer SAVE for the parent node. This

procedure will also be used in the next section, or deletion.

Searching

Algorithm: FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)

A binary tree T is in memory and an ITEM of information is given. This procedure finds the

location LOC of ITEM in T and also the location PAR of the parent of ITEM. There are three

special cases:

(i) LOC = NULL and PAR = NULL will indicate that the tree is empty. [No node.]

(ii) LOC != NULL and PAR = NULL will indicate that ITEM is root of T. [No parent of node, i.e.,

only one node.]

(iii) LOC = NULL and PAR != NULL will indicate that ITEM is not in T and can be added to T as

a child of the node N with the location PAR.

Step I: [Tree empty?]

If ROOT = NULL, then:

Set LOC := NULL and PAR := NULL, and Return.

Step II: [ITEM at root?]

If ITEM = INFO[ROOT], then:

Set LOC := ROOT and PAR := NULL, and Return.

Step III: [Initialize pointers PTR and SAVE.]

If ITEM < INFO[ROOT], then:

. Set SAVE := ROOT and PTR := LEFT[ROOT] Else: Set SAVE := ROOT and PTR := RIGHT[ROOT] [End of If structure. Else: Set SAVE := PTR and PTR := RIGHT[PTR].] Step VII: [Search unsuccessful.] Set LOC := NULL and PAR := SAVE. [End of If structure. Step VI: If ITEM < INFO[PTR]. Step VIII: Return. then: Set SAVE := PTR and PTR := LEFT[PTR].] [End of step 4 loop. Step V: [ITEM found?] If ITEM := INFO[PTR]. then: Set LOC := PTR and PAR := SAVE. and Return.] Step IV: Repeat steps 5 and 6 while PTR != NULL.

- 9A05302 Advanced Data StructuresUploaded bysivabharathamurthy
- So Lns Midterm Pr AcquesUploaded byGobara Dhan
- Lab Course-I SlipsUploaded byRK NAIR
- AVL-Tree-Deletion.pdfUploaded byRajContent
- TREE (C)Uploaded byबानि तमिन्
- Int QuestionsUploaded bysrseshan
- 1Uploaded bybgstatistics
- avl_treeUploaded byPapori Borgohain
- Noise Removal Efficient Web Data MiningUploaded byJyothi_B_Pillai
- Lecture Notes COMP3506Uploaded byJack
- AVL Tree OperationUploaded byKuldeepSinghRana
- DM Lecture - TreeUploaded byAshik Haque
- lec9Uploaded byMatthew John Sino Cruz
- Data Structures QuestionsUploaded byraman.r.bhardwaj
- AVL Tree NotesUploaded bysonal
- BinomialHeapsUploaded bykajendran4958
- Lecture4p_1ppUploaded byMrZaggy
- Decision TreeUploaded byAnup Bhattarai
- Openvdb IntroductionUploaded byRafael Campos
- Lecture 16Uploaded byMoy Rg
- Efficiently Delivering Data Packets Using Distributed Protocol for Runtime Groups Formed In Peer-to-Peer NetworkUploaded byWhite Globe Publications (IJORCS)
- Tide ManualUploaded byMario Ayil Huicab
- 0.Red Black TreesUploaded byHow My
- journal for scribdUploaded byIlman Samhabib
- Red Black TreesUploaded byAnonymous 1EHtPKVjl
- Chap15WEUploaded byMohan Bisht
- Cs 301 Final Term Solved Paper 2Uploaded byRizwanIlyas
- c++ Lab MannualUploaded byDasaradha Rami Reddy V
- Shmuel Lifsches and Saharon Shelah- Uniformization, choice functions and well orders in the class of treesUploaded byJgfm2
- 10.1.1.7.4678 @robUploaded bySuman Bhowmick

- Role of ManagersUploaded byRahul Upamanyu
- FlowchartUploaded byRahul Upamanyu
- RahulUploaded byRahul Upamanyu
- Linked ListUploaded byRahul Upamanyu
- What is C.docxUploaded byRahul Upamanyu
- Insertion Into Binary Search TreeUploaded byRahul Upamanyu
- linkedlist.docUploaded byRahul Upamanyu
- tutorial%5CLINKED LIST WIKIPEDIA.docUploaded byRahul Upamanyu
- CSS PropertiesUploaded byRahul Upamanyu
- Variables 6Uploaded byRahul Upamanyu
- Font PropertiesUploaded byRahul Upamanyu
- Including CSS in HTML DocumentUploaded byRahul Upamanyu
- Font PropertiessUploaded byRahul Upamanyu
- CSS IntroductionUploaded byRahul Upamanyu
- Himani Gupta (1)Uploaded byRahul Upamanyu