+ All Categories
Home > Documents > CSCS-200 Data Structure and Algorithms Lecture-19-20.

CSCS-200 Data Structure and Algorithms Lecture-19-20.

Date post: 06-Jan-2018
Category:
Upload: harry-wood
View: 222 times
Download: 0 times
Share this document with a friend
Description:
3 Level-order Traversal Level-order:
81
CSCS-200 Data Structure and Algorithms Lecture-19-20
Transcript
Page 1: CSCS-200 Data Structure and Algorithms Lecture-19-20.

CSCS-200 Data Structure and Algorithms

Lecture-19-20

Page 2: CSCS-200 Data Structure and Algorithms Lecture-19-20.

2

Level-order Traversal

• There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously.

• In level-order traversal, we visit the nodes at each level before proceeding to the next level.

• At each level, we visit the nodes in a left-to-right order.

Page 3: CSCS-200 Data Structure and Algorithms Lecture-19-20.

3

Level-order Traversal

Level-order: 14 4 15 3 9 18 7 16 20 5 17

14

4

9

7

3

5

15

18

16 20

17

Page 4: CSCS-200 Data Structure and Algorithms Lecture-19-20.

4

Level-order Traversal

• How do we do level-order traversal?• Surprisingly, if we use a queue instead of

a stack, we can visit the nodes in level-order.

• Here is the code for level-order traversal:

Page 5: CSCS-200 Data Structure and Algorithms Lecture-19-20.

5

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 6: CSCS-200 Data Structure and Algorithms Lecture-19-20.

6

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 7: CSCS-200 Data Structure and Algorithms Lecture-19-20.

7

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 8: CSCS-200 Data Structure and Algorithms Lecture-19-20.

8

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 9: CSCS-200 Data Structure and Algorithms Lecture-19-20.

9

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 10: CSCS-200 Data Structure and Algorithms Lecture-19-20.

10

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 11: CSCS-200 Data Structure and Algorithms Lecture-19-20.

11

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 12: CSCS-200 Data Structure and Algorithms Lecture-19-20.

12

Level-order Traversalvoid levelorder(TreeNode<int>* treeNode){ Queue<TreeNode<int>* > q; if( treeNode == NULL ) return; q.enqueue( treeNode); while( !q.empty() ) { treeNode = q.dequeue(); cout << *(treeNode->getInfo()) << " "; if(treeNode->getLeft() != NULL )

q.enqueue( treeNode->getLeft()); if(treeNode->getRight() != NULL )

q.enqueue( treeNode->getRight()); } cout << endl;}

Page 13: CSCS-200 Data Structure and Algorithms Lecture-19-20.

13

Level-order Traversal

Queue: 14Output:

14

4

9

7

3

5

15

18

16 20

17

Page 14: CSCS-200 Data Structure and Algorithms Lecture-19-20.

14

Level-order Traversal

Queue: 4 15Output: 14

14

4

9

7

3

5

15

18

16 20

17

Page 15: CSCS-200 Data Structure and Algorithms Lecture-19-20.

15

Level-order Traversal

Queue: 15 3 9Output: 14 4

14

4

9

7

3

5

15

18

16 20

17

Page 16: CSCS-200 Data Structure and Algorithms Lecture-19-20.

16

Level-order Traversal

Queue: 3 9 18Output: 14 4 15

14

4

9

7

3

5

15

18

16 20

17

Page 17: CSCS-200 Data Structure and Algorithms Lecture-19-20.

17

Level-order Traversal

Queue: 9 18Output: 14 4 15 3

14

4

9

7

3

5

15

18

16 20

17

Page 18: CSCS-200 Data Structure and Algorithms Lecture-19-20.

18

Level-order Traversal

Queue: 18 7Output: 14 4 15 3 9

14

4

9

7

3

5

15

18

16 20

17

Page 19: CSCS-200 Data Structure and Algorithms Lecture-19-20.

19

Level-order Traversal

Queue: 7 16 20Output: 14 4 15 3 9 18

14

4

9

7

3

5

15

18

16 20

17

Page 20: CSCS-200 Data Structure and Algorithms Lecture-19-20.

20

Level-order Traversal

Queue: 16 20 5Output: 14 4 15 3 9 18 7

14

4

9

7

3

5

15

18

16 20

17

Page 21: CSCS-200 Data Structure and Algorithms Lecture-19-20.

21

Level-order Traversal

Queue: 20 5 17Output: 14 4 15 3 9 18 7 16

14

4

9

7

3

5

15

18

16 20

17

Page 22: CSCS-200 Data Structure and Algorithms Lecture-19-20.

22

Level-order Traversal

Queue: 5 17Output: 14 4 15 3 9 18 7 16 20

14

4

9

7

3

5

15

18

16 20

17

Page 23: CSCS-200 Data Structure and Algorithms Lecture-19-20.

23

Level-order Traversal

Queue: 17Output: 14 4 15 3 9 18 7 16 20 5

14

4

9

7

3

5

15

18

16 20

17

Page 24: CSCS-200 Data Structure and Algorithms Lecture-19-20.

24

Level-order Traversal

Queue:Output: 14 4 15 3 9 18 7 16 20 5 17

14

4

9

7

3

5

15

18

16 20

17

Page 25: CSCS-200 Data Structure and Algorithms Lecture-19-20.

25

Storing other Type of Data

• The examples of binary trees so far have been storing integer data in the tree node.

• This is surely not a requirement. Any type of data can be stored in a tree node.

• Here, for example, is the C++ code to build a tree with character strings.

Page 26: CSCS-200 Data Structure and Algorithms Lecture-19-20.

26

Binary Search Tree with Stringsvoid wordTree(){

TreeNode<char>* root = new TreeNode<char>();static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL};root->setInfo( word[0] );

for(i=1; word[i]; i++ )

insert(root, word[i] );inorder( root ); cout << endl;

}

Page 27: CSCS-200 Data Structure and Algorithms Lecture-19-20.

27

Binary Search Tree with Stringsvoid wordTree(){

TreeNode<char>* root = new TreeNode<char>();static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL};root->setInfo( word[0] );

for(i=1; word[i]; i++ )

insert(root, word[i] );inorder( root ); cout << endl;

}

Page 28: CSCS-200 Data Structure and Algorithms Lecture-19-20.

28

Binary Search Tree with Stringsvoid wordTree(){

TreeNode<char>* root = new TreeNode<char>();static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL};root->setInfo( word[0] );

for(i=1; word[i]; i++ )

insert(root, word[i] );inorder( root ); cout << endl;

}

Page 29: CSCS-200 Data Structure and Algorithms Lecture-19-20.

29

Binary Search Tree with Stringsvoid wordTree(){

TreeNode<char>* root = new TreeNode<char>();static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL};root->setInfo( word[0] );

for(i=1; word[i]; i++ )

insert(root, word[i] );inorder( root ); cout << endl;

}

Page 30: CSCS-200 Data Structure and Algorithms Lecture-19-20.

30

Binary Search Tree with Stringsvoid wordTree(){

TreeNode<char>* root = new TreeNode<char>();static char* word[] = "babble", "fable", "jacket", "backup", "eagle","daily","gain","bandit","abandon", "abash","accuse","economy","adhere","advise","cease", "debunk","feeder","genius","fetch","chain", NULL};root->setInfo( word[0] );

for(i=1; word[i]; i++ )

insert(root, word[i] );inorder( root ); cout << endl;

}

Page 31: CSCS-200 Data Structure and Algorithms Lecture-19-20.

31

Binary Search Tree with Stringsvoid insert(TreeNode<char>* root, char* info){ TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Page 32: CSCS-200 Data Structure and Algorithms Lecture-19-20.

32

Binary Search Tree with Stringsvoid insert(TreeNode<char>* root, char* info){ TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Page 33: CSCS-200 Data Structure and Algorithms Lecture-19-20.

33

Binary Search Tree with Stringsvoid insert(TreeNode<char>* root, char* info){ TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Page 34: CSCS-200 Data Structure and Algorithms Lecture-19-20.

34

Binary Search Tree with Stringsvoid insert(TreeNode<char>* root, char* info){ TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Page 35: CSCS-200 Data Structure and Algorithms Lecture-19-20.

35

Binary Search Tree with Stringsvoid insert(TreeNode<char>* root, char* info){ TreeNode<char>* node = new TreeNode<char>(info); TreeNode<char> *p, *q; p = q = root; while( strcmp(info, p->getInfo()) != 0 && q != NULL ) { p = q; if( strcmp(info, p->getInfo()) < 0 ) q = p->getLeft(); else q = p->getRight(); }

Page 36: CSCS-200 Data Structure and Algorithms Lecture-19-20.

36

Binary Search Tree with Stringsif( strcmp(info, p->getInfo()) == 0 ){

cout << "attempt to insert duplicate: " << *info << endl; delete node;

}else if( strcmp(info, p->getInfo()) < 0 )

p->setLeft( node );else

p->setRight( node ); }

Page 37: CSCS-200 Data Structure and Algorithms Lecture-19-20.

37

Binary Search Tree with Stringsif( strcmp(info, p->getInfo()) == 0 ){

cout << "attempt to insert duplicate: " << *info << endl; delete node;

}else if( strcmp(info, p->getInfo()) < 0 )

p->setLeft( node );else

p->setRight( node ); }

Page 38: CSCS-200 Data Structure and Algorithms Lecture-19-20.

38

Binary Search Tree with Stringsif( strcmp(info, p->getInfo()) == 0 ){

cout << "attempt to insert duplicate: " << *info << endl; delete node;

}else if( strcmp(info, p->getInfo()) < 0 )

p->setLeft( node );else

p->setRight( node ); }

Page 39: CSCS-200 Data Structure and Algorithms Lecture-19-20.

39

Binary Search Tree with StringsOutput:

abandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Page 40: CSCS-200 Data Structure and Algorithms Lecture-19-20.

40

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

Page 41: CSCS-200 Data Structure and Algorithms Lecture-19-20.

41

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

This should not come as a surprise if you consider how we built the BST.

Page 42: CSCS-200 Data Structure and Algorithms Lecture-19-20.

42

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

This should not come as a surprise if you consider how we built the BST.

For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.

Page 43: CSCS-200 Data Structure and Algorithms Lecture-19-20.

43

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

This should not come as a surprise if you consider how we built the BST.

For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.

Inorder prints the left subtree, then the node finally the right subtree.

Page 44: CSCS-200 Data Structure and Algorithms Lecture-19-20.

44

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

This should not come as a surprise if you consider how we built the BST.

For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.

Inorder prints the left subtree, then the node finally the right subtree.

Building a BST and doing an inorder traversal leads to a sorting algorithm.

Page 45: CSCS-200 Data Structure and Algorithms Lecture-19-20.

45

Binary Search Tree with Stringsabandonabashaccuseadhereadvisebabblebackupbanditceasechaindailydebunkeagleeconomyfablefeederfetchgaingeniusjacket

Notice that the words are sorted in increasing order when we traversed the tree in inorder manner.

This should not come as a surprise if you consider how we built the BST.

For a given node, values less than the info in the node were all in the left subtree and values greater or equal were in the right.

Inorder prints the left subtree, then the node finally the right subtree.

Building a BST and doing an inorder traversal leads to a sorting algorithm.

Page 46: CSCS-200 Data Structure and Algorithms Lecture-19-20.

46

Deleting a node in BST

• As is common with many data structures, the hardest operation is deletion.

• Once we have found the node to be deleted, we need to consider several possibilities.

• If the node is a leaf, it can be deleted immediately.

Page 47: CSCS-200 Data Structure and Algorithms Lecture-19-20.

47

Deleting a node in BST• If the node has one child, the node can be

deleted after its parent adjusts a pointer to bypass the node and connect to inorder successor.

6

2

4

3

1

8

Page 48: CSCS-200 Data Structure and Algorithms Lecture-19-20.

48

Deleting a node in BST

• The inorder traversal order has to be maintained after the delete.

6

2

4

3

1

8

6

2

4

3

1

8

Page 49: CSCS-200 Data Structure and Algorithms Lecture-19-20.

49

Deleting a node in BST

• The inorder traversal order has to be maintained after the delete.

6

2

4

3

1

8

6

2

4

3

1

8

6

2

31

8

Page 50: CSCS-200 Data Structure and Algorithms Lecture-19-20.

50

Deleting a node in BST

• The complicated case is when the node to be deleted has both left and right subtrees.

• The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node.

Page 51: CSCS-200 Data Structure and Algorithms Lecture-19-20.

51

Deleting a node in BSTDelete(2): locate inorder successor

6

2

5

3

1

8

4Inorder successor

Page 52: CSCS-200 Data Structure and Algorithms Lecture-19-20.

52

Deleting a node in BSTDelete(2): locate inorder successor

6

2

5

3

1

8

4Inorder successor

Inorder successor will be the left-most node in the right subtree of 2.

The inorder successor will not have a left child because if it did, that child would be the left-most node.

Page 53: CSCS-200 Data Structure and Algorithms Lecture-19-20.

53

Deleting a node in BSTDelete(2): copy data from inorder successor

6

2

5

3

1

8

4

6

3

5

3

1

8

4

Page 54: CSCS-200 Data Structure and Algorithms Lecture-19-20.

54

Deleting a node in BSTDelete(2): remove the inorder successor

6

2

5

3

1

8

4

6

3

5

3

1

8

4

6

3

5

3

1

8

4

Page 55: CSCS-200 Data Structure and Algorithms Lecture-19-20.

55

Deleting a node in BSTDelete(2)

6

3

5

4

1

8

6

3

5

3

1

8

4

Page 56: CSCS-200 Data Structure and Algorithms Lecture-19-20.

56

C++ code for delete

• ‘delete’ is C++ keyword. We will call our deleteNode routine remove.

• Here is the C++ code for remove.

Page 57: CSCS-200 Data Structure and Algorithms Lecture-19-20.

57

C++ code for delete

TreeNode<int>* remove(TreeNode<int>* tree, int info){ TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

Page 58: CSCS-200 Data Structure and Algorithms Lecture-19-20.

58

C++ code for delete

TreeNode<int>* remove(TreeNode<int>* tree, int info){ TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

Page 59: CSCS-200 Data Structure and Algorithms Lecture-19-20.

59

C++ code for delete

TreeNode<int>* remove(TreeNode<int>* tree, int info){ TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

Page 60: CSCS-200 Data Structure and Algorithms Lecture-19-20.

60

C++ code for delete

TreeNode<int>* remove(TreeNode<int>* tree, int info){ TreeNode<int>* t; int cmp = info - *(tree->getInfo()); if( cmp < 0 ){ t = remove(tree->getLeft(), info); tree->setLeft( t ); } else if( cmp > 0 ){ t = remove(tree->getRight(), info); tree->setRight( t ); }

Page 61: CSCS-200 Data Structure and Algorithms Lecture-19-20.

61

C++ code for delete

//two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

Page 62: CSCS-200 Data Structure and Algorithms Lecture-19-20.

62

C++ code for delete

//two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

Page 63: CSCS-200 Data Structure and Algorithms Lecture-19-20.

63

C++ code for delete

//two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

Page 64: CSCS-200 Data Structure and Algorithms Lecture-19-20.

64

C++ code for delete

//two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

Page 65: CSCS-200 Data Structure and Algorithms Lecture-19-20.

65

C++ code for delete

//two children, replace with inorder successor else if(tree->getLeft() != NULL && tree->getRight() != NULL ){ TreeNode<int>* minNode; minNode = findMin(tree->getRight()); tree->setInfo( minNode->getInfo() ); t = remove(tree->getRight(), *(minNode->getInfo())); tree->setRight( t ); }

Page 66: CSCS-200 Data Structure and Algorithms Lecture-19-20.

66

C++ code for delete

else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree;}

Page 67: CSCS-200 Data Structure and Algorithms Lecture-19-20.

67

C++ code for delete

else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree;}

Page 68: CSCS-200 Data Structure and Algorithms Lecture-19-20.

68

C++ code for delete

else { // case 1 TreeNode<int>* nodeToDelete = tree; if( tree->getLeft() == NULL ) //will handle 0 children tree = tree->getRight(); else if( tree->getRight() == NULL ) tree = tree->getLeft(); else tree = NULL; delete nodeToDelete; } return tree;}

Page 69: CSCS-200 Data Structure and Algorithms Lecture-19-20.

69

C++ code for delete

TreeNode<int>* findMin(TreeNode<int>* tree){ if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() );}

Page 70: CSCS-200 Data Structure and Algorithms Lecture-19-20.

70

C++ code for delete

TreeNode<int>* findMin(TreeNode<int>* tree){ if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() );}

Page 71: CSCS-200 Data Structure and Algorithms Lecture-19-20.

71

C++ code for delete

TreeNode<int>* findMin(TreeNode<int>* tree){ if( tree == NULL ) return NULL; if( tree->getLeft() == NULL ) return tree; // this is it. return findMin( tree->getLeft() );}

Page 72: CSCS-200 Data Structure and Algorithms Lecture-19-20.

72

BinarySearchTree.h

Let us design the BinarySearchTree class (factory).

Page 73: CSCS-200 Data Structure and Algorithms Lecture-19-20.

73

BinarySearchTree.h

#ifndef _BINARY_SEARCH_TREE_H_#define _BINARY_SEARCH_TREE_H_#include <iostream.h> // For NULL

// Binary node and forward declarationtemplate <class EType>class BinarySearchTree;

Page 74: CSCS-200 Data Structure and Algorithms Lecture-19-20.

74

BinarySearchTree.h

#ifndef _BINARY_SEARCH_TREE_H_#define _BINARY_SEARCH_TREE_H_#include <iostream.h> // For NULL

// Binary node and forward declarationtemplate <class EType>class BinarySearchTree;

Page 75: CSCS-200 Data Structure and Algorithms Lecture-19-20.

75

BinarySearchTree.h

template <class EType>class BinaryNode{ EType element; BinaryNode *left; BinaryNode *right;

BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>;};

Page 76: CSCS-200 Data Structure and Algorithms Lecture-19-20.

76

BinarySearchTree.h

template <class EType>class BinaryNode{ EType element; BinaryNode *left; BinaryNode *right;

BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>;};

Page 77: CSCS-200 Data Structure and Algorithms Lecture-19-20.

77

BinarySearchTree.h

template <class EType>class BinaryNode{ EType element; BinaryNode *left; BinaryNode *right;

BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>;};

Page 78: CSCS-200 Data Structure and Algorithms Lecture-19-20.

78

BinarySearchTree.h

template <class EType>class BinaryNode{ EType element; BinaryNode *left; BinaryNode *right;

BinaryNode( const EType & theElement, BinaryNode *lt, BinaryNode *rt ) : element( theElement ), left( lt ), right( rt ) { } friend class BinarySearchTree<EType>;};

Page 79: CSCS-200 Data Structure and Algorithms Lecture-19-20.

79

BinarySearchTree.h

template <class EType>class BinarySearchTree{public: BinarySearchTree( const EType& notFound ); BinarySearchTree( const BinarySearchTree& rhs ); ~BinarySearchTree( );

const EType& findMin( ) const; const EType& findMax( ) const; const EType& find( const EType & x ) const; bool isEmpty( ) const; void printInorder( ) const;

Page 80: CSCS-200 Data Structure and Algorithms Lecture-19-20.

80

BinarySearchTree.h

void insert( const EType& x ); void remove( const EType& x );

const BinarySearchTree & operator= ( const BinarySearchTree & rhs );

Page 81: CSCS-200 Data Structure and Algorithms Lecture-19-20.

81

BinarySearchTree.h

private: BinaryNode<EType>* root; // ITEM_NOT_FOUND object used to signal failed finds const EType ITEM_NOT_FOUND;

const EType& elementAt( BinaryNode<EType>* t ); void insert(const EType& x, BinaryNode<EType>* & t); void remove(const EType& x, BinaryNode<EType>* & t); BinaryNode<EType>* findMin(BinaryNode<EType>* t); BinaryNode<EType>* findMax(BinaryNode<EType>* t); BinaryNode<EType>* find(const EType& x, BinaryNode<EType>* t ); void makeEmpty(BinaryNode<EType>* & t); void printInorder(BinaryNode<EType>* t);};

#endif


Recommended