Binary Search Tree

Search From a Tree • How much time it takes? • In order to search an element from the tree of n nodes, we may have to perform maximum n comparison. • What are the advantages of storing data in the form of tree? • Move towards search tree.

Binary Search Tree •

A binary tree that is either empty or in which every node contains a key and satisfies the conditions: I.

The key in the left child of a node (if it exists) is less than the key in its parent II. The key in the right child of a node (if it exists) is greater than the key in its parent node. III. The left and right sub-trees of the root are again binary search trees.



Keys in a binary search tree can be regarded as already sorted into order

Making a Binary Search Tree 4

15

20

15

null

15 4

17 15

15 4

20

15

19

4

4 20

17

20 17 19

Binary Search Tree Examples Binary Search Trees: What about this tree?

6 2

8

1

4 3 6

What about this tree?

2

8

1

4 3

7

Implementation of Binary Search Tree class BTNode{ public: int data; BTNode *left,*right; BTNode (int d,BTNode* l=NULL, BTNode* r=NULL) {data=d;left=l; right=r;} };

Implementation of Binary Search Tree class BST{ BTNode * root; public: BST(){root=NULL;} void InsertKey(int x); void InsertRKey(int x,BTNode* & B); void Inorder(BTNode* bt); void Preorder(BTNode* bt); void Postorder(BTNode* bt); BTNode* &RRoot(){return root;} void deleteNode(BTNode *BT); BTNode* SearchKey(int d); ~ BST( ); }

Continue on next slide…

Deleting a Binary Search Tree void BST::deleteNode(BTNode *BT) { if(BT!=NULL) { deleteNode(BT->left); deleteNode(BT->right); cout<<"\tdeleting\t"<data; delete BT; } } BST:: ~ BST() { deleteNode(RRoot()); }

Implementation of Binary Search Tree void BST::Inorder(BTNode* bt) { if(bt != NULL) { Inorder(bt->left); cout<<"\t"<data; Inorder(bt->right); } } void BST::Preorder(BTNode* bt) { if(bt != NULL) { cout<<"\t"<data; Preorder(bt->left); Preorder(bt->right); } }

void BST::Postorder(BTNode* bt) { if(bt != NULL) { Postorder(bt->left); Postorder(bt->right); cout<<"\t"<data; } }

Searching a Binary Search Tree • For every node, compare the key to be located with the value stored in the node currently pointed at. • If the key is less than the value, go to the left subtree and try again. • If the key is greater than the value, go to the right subtree and try again. • If the key is equal to the value then, obviously the search can be discontinued. • The search is also aborted if there is no way to go, indicating that key is not in the tree.

Searching a Binary Search Tree BTNode* BST::SearchKey(int d) { BTNode *TN=root; while(TN !=NULL) { if(TN->data == d) return TN; else if( TN->data < d) TN=TN->right; else TN=TN->left; } return NULL; }

BST Insertion void BST::InsertKey( int x) { BTNode* pZ=new BTNode(x); if(root ==NULL) root=pZ; else { BTNode* Pre, *Cur=root; while(Cur!=NULL) { Pre=Cur; if(x < Cur->data) else } if(x < Pre->data) else } }

Cur=Cur->left; Cur=Cur->right; Pre->left =pZ; Pre->right=pZ;

BST Insertion (Recursive) void BST:: InsertRKey(int x, BTNode* & B) { if(B==NULL) B=new BTNode(x); else if(x < B->data) InsertRKey(x,B->left); else InsertRKey(x,B->right); }

Driver int main ( ) { BST b1; int d; BTNode * Temp; while(1) { cout<<"Enter Next Node "; cin>>d; if(d==0) break; b1.InsertRKey(d , b1.RRoot()); } cout<<"\n\n\t\t The Inorder traversal is\n "; b1.Inorder(b1.RRoot()); cout<<"Enter the key to search "; cin>>d; Temp=b1.SearchKey(d); if(Temp!=NULL) cout<<"\n"<data<<" Found at Address "<

Lecture_7 Binary Search Tree-studywing.blogspot.com.pdf ...

There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Lecture_7 ...

122KB Sizes 0 Downloads 202 Views

Recommend Documents

Interlaced Binary Search: Multiple Items' Binary Search ...
and data encoding. Index Terms— ... (A/D) (Figure-1and Table-3) and (2) encoding of binary data using fewer bits ...... minimum allowable sampling rate 2fM for exact recovery of a .... Systems” Singapore, McGraw-Hill Book Company 1986.

Optimal binary search trees -
Optimal binary search trees. ❖ n identifiers : x. 1.

Quicksort and Binary Search Algorithms
throughout the programming work when you need to sort a set of data and ..... sorting process, it's observed that in practice when the records are big, that is, have a great .... searching method because right in the moment that we made an initial an

Lecture_8 Binary Search Tree Deletion-studywing.blogspot.com.pdf ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Lecture_8 ...

lecture7.pdf
I Receive endowment. I Can borrow (or lend) internationally at world interest rate (SOE). Financial friction: Borrowing subject to fraction of value of collateral.

Lecture7.pdf
Controllability deals with whether or not the state of. a state-space equation can be controlled from the. input. – Consider a state equation. This state equation or ...

Optimizing Binary Fisher Codes for Visual Search - IEEE Xplore
The Institute of Digital Media, Peking University, Beijing, China. {zhew,lingyu,linjie,cjie,tjhuang,wgao}@pku.edu.cn. Fisher vectors (FV), a global representation obtained by aggregating local invari- ant features (e.g., SIFT), generates the state-of

optimal binary search tree using dynamic programming pdf ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. optimal binary ...

binary search tree program in c pdf
binary search tree program in c pdf. binary search tree program in c pdf. Open. Extract. Open with. Sign In. Main menu. Displaying binary search tree program in ...

optimal binary search tree using dynamic programming pdf ...
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

lecture7/Gestures/Gestures/AppDelegate.h // 1. // AppDelegate ... - cs164
111. 112. // listen for right swipe. 113. ... listen for left swipe. 118. swipeGesture ...... CGRect square = CGRectMake(0.0f, 0.0f, 10.0f, 60.0f);. 18. [[UIColor ...

lecture7/Gestures/Gestures/AppDelegate.h // 1. // AppDelegate.h 2 ...
Demonstrates Core Graphics with a rectangle. 9. //. 10. 11. #import . 12. 13. @class ViewController;. 14. 15. @interface AppDelegate : UIResponder . 16. 17. @property (strong, nonatomic) ViewController *viewController;. 18. @property (strong, nonatom

binary search.pdf
Page 1 of 2. Stand 02/ 2000 MULTITESTER I Seite 1. RANGE MAX/MIN VoltSensor HOLD. MM 1-3. V. V. OFF. Hz A. A. °C. °F. Hz. A. MAX. 10A. FUSED.

the search for tight binary radio pulsars in arecibo radio data with ...
tight binary pulsars in radio data from the Arecibo telescope op- erated by Cornell ... data conditioning: narrow- and wideband removal of strong burstlike and ...

BINARY OPERATOR OVERLOADING.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

Circulant Binary Embedding - Sanjiv Kumar
to get the binary code: h(x) = sign(RT. 1 ZR2). (2). When the shapes of Z, R1, R2 are chosen appropriately, the method has time and space complexity of O(d1.5) ...

A201720605_Numerical Binary Search.pdf
Jan 18, 2017 - Binary Search Algorithm. Page 2 of 2. A201720605_Numerical Binary Search.pdf. A201720605_Numerical Binary Search.pdf. Open. Extract.

Binary Numbers PDF.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

Swift Navigation Binary Protocol - GitHub
RTK accuracy with legacy host hardware or software that can only read NMEA, recent firmware ..... search space with the best signal-to-noise (SNR) ratio.

Binary Numbers PDF.pdf
Page 1 of 1. Binary Numbers PDF.pdf. Binary Numbers PDF.pdf. Open. Extract. Open with. Sign In. Main menu. Displaying Binary Numbers PDF.pdf. Page 1 of 1.

Binary - Ternary FORM.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying.

Overcoming the Binary Logic of Adoption
IPTV in Deutschland und Österreich (pp. 241–255). Baden-Baden, Germany: Nomos. ... New Media Society, 1(1), 83–100. Lowery, S. A., & DeFleur, M. L. (Eds.).