DS Lec 11 BST

11

Click here to load reader

Transcript of DS Lec 11 BST

Page 1: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 1/11

 

Binary Search Tree

Lecture # 11

Page 2: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 2/11

 

Search From a Tree

• How much time it takes?

• In order to search an element from the tree

of n nodes, we may hae to !erform

ma"imum n com!arison

• $hat are the adanta%es of storin% data in

the form of tree?

• &oe towards search tree

Page 3: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 3/11

 

Binary Search Tree

• ' (inary tree that is either em!ty or in whicheery node contains a key and satisfies theconditions)

I The key in the left child of a node *if it e"ists+ is lessthan the key in its !arent

II The key in the ri%ht child of a node *if it e"ists+ is%reater than the key in its !arent node

III The left and ri%ht su(trees of the root are a%ain

 (inary search trees

• -eys in a (inary search tree can (e re%arded asalready sorted into order 

Page 4: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 4/11

 

&akin% a Binary Search Tree

null

1. /

1.

0

1.

/

12

1.

/ 0

13

1.

/ 0

12

1.

/ 0

12

13

Page 5: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 5/11

 

Binary Search TreeExamples Binary Search Trees:

$hat a(out this tree? 4

50

1 /

6

$hat a(out this tree?

4

50

1 /

6 2

Page 6: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 6/11

 

Im!lementation of Binary Search Tree

template<class T>

class BSTNode

{

public:

T info;BSTNode *left, *right;

BSTNode(:info(!, left(!, right(! { "

BSTNode(T el, BSTNode *lef # !, BSTNode *ret # !

{

info # el;

left # lef;

right # ret;

"

";

Page 7: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 7/11

 

Im!lementation of Binary Search Tree

template<class T>

class BST

{

pri$ate:

BSTNode<T> *root;

$oid clear(BSTNode<T> *;

$oid preorder(BSTNode<T> *;

$oid inorder(BSTNode<T> *;

$oid postorder(BSTNode<T> *;

public:

BST( { root # !; "%BST( { clear( ; root # !; "

$oid clear( { clear(root; root # !; " &&'elete all nodes of a tree

bool ismpt)( { return root ## !; " &&checs tree is empt) or not

7ontinue on ne"t slide8

Page 8: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 8/11

 

Im!lementation of Binary Search Tree

$oid preorder( { preorder(root; " &&preorder tra$ersal of tree

$oid inorder( { inorder(root; " &&inorder tra$ersal of tree

$oid postorder( { postorder(root; " &&postorder tra$eral of tree

BSTNode<T> * search(T el; &&search an element$oid insert(T el; &&+nsert an element in BST

$oid deleteB)erging(BSTNode<T> *; &&delete a node from BST&&b) merging

$oid deleteB)-op)ing(BSTNode<T> * &&delete an element&&from BST b) cop)ing

";

Page 9: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 9/11

 

Searchin% a Binary Search Tree

• For eery node, com!are the key to (e located withthe alue stored in the node currently !ointed at

•If the key is less than the alue, %o to the left su(treeand try a%ain

• If the key is %reater than the alue, %o to the ri%htsu(tree and try a%ain

• If the key is e9ual to the alue then, o(iously thesearch can (e discontinued

• The search is also a(orted if there is no way to %o,indicatin% that key is not in the tree

Page 10: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 10/11

 

Searchin% a Binary Search Tree

template<class T>

BSTNode<T>* BST<T>::search(T ele

{

BSTNode<T> *s#root;

.hile(s/#!

{if(ele##s0>info

return s;

else if(ele < s0>info

s # s0>left;

else

s # s0>right;

"

return !;

"

Page 11: DS Lec 11 BST

7/23/2019 DS Lec 11 BST

http://slidepdf.com/reader/full/ds-lec-11-bst 11/11

 

BST Insertion

template<class T>$oid BST<T>::insert(T ele {

if(root##! root#ne. BSTNode<T>(ele;

else {

BSTNode<T> *p#root, *pre$#!;

.hile(p/#! { && find a place for inserting ne. node

pre$#p;

if(ele < p0>info p # p0>left;

else if(ele > p0>info p # p0>right;

else p#!;

"

if(ele < pre$0>info pre$0>left # ne. BSTNode<T>(ele;else if(ele > pre$0>info pre$0>right # ne. BSTNode<T>(ele;

"

"