Computer notes - Binary Search Tree with Strings

3,607 views
3,456 views

Published on

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.

Published in: Education, Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,607
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
198
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • End of lecture 14
  • Start lecture 16
  • End of lecture 15
  • Computer notes - Binary Search Tree with Strings

    1. 1. Class No.15 Data Structures http://ecomputernotes.com
    2. 2. Level-order Traversal <ul><li>There is yet another way of traversing a binary tree that is not related to recursive traversal procedures discussed previously. </li></ul><ul><li>In level-order traversal, we visit the nodes at each level before proceeding to the next level. </li></ul><ul><li>At each level, we visit the nodes in a left-to-right order. </li></ul>http://ecomputernotes.com
    3. 3. Level-order Traversal Level-order: 14 4 15 3 9 18 7 16 20 5 17 http://ecomputernotes.com 14 4 9 7 3 5 15 18 16 20 17
    4. 4. Level-order Traversal <ul><li>How do we do level-order traversal? </li></ul><ul><li>Surprisingly, if we use a queue instead of a stack, we can visit the nodes in level-order. </li></ul><ul><li>Here is the code for level-order traversal: </li></ul>http://ecomputernotes.com
    5. 5. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    6. 6. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    7. 7. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    8. 8. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    9. 9. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    10. 10. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    11. 11. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    12. 12. Level-order Traversal <ul><li>void levelorder(TreeNode<int>* treeNode) </li></ul><ul><li>{ </li></ul><ul><li>Queue<TreeNode<int>* > q; </li></ul><ul><li>if( treeNode == NULL ) return; </li></ul><ul><li>q.enqueue( treeNode); </li></ul><ul><li>while( !q.empty() ) </li></ul><ul><li>{ </li></ul><ul><li>treeNode = q.dequeue(); </li></ul><ul><li>cout << *(treeNode->getInfo()) << &quot; &quot;; </li></ul><ul><li>if(treeNode->getLeft() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getLeft()); </li></ul><ul><li>if(treeNode->getRight() != NULL ) </li></ul><ul><li>q.enqueue( treeNode->getRight()); </li></ul><ul><li>} </li></ul><ul><li>cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    13. 13. Level-order Traversal Queue: 14 Output: 14 http://ecomputernotes.com 4 9 7 3 5 15 18 16 20 17
    14. 14. Level-order Traversal Queue: 4 15 Output: 14 14 4 15 http://ecomputernotes.com 9 7 3 5 18 16 20 17
    15. 15. Level-order Traversal Queue: 15 3 9 Output: 14 4 14 4 9 3 15 http://ecomputernotes.com 7 5 18 16 20 17
    16. 16. Level-order Traversal Queue: 3 9 18 Output: 14 4 15 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    17. 17. Level-order Traversal Queue: 9 18 Output: 14 4 15 3 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    18. 18. Level-order Traversal Queue: 18 7 Output: 14 4 15 3 9 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    19. 19. Level-order Traversal Queue: 7 16 20 Output: 14 4 15 3 9 18 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    20. 20. Level-order Traversal Queue: 16 20 5 Output: 14 4 15 3 9 18 7 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    21. 21. Level-order Traversal Queue: 20 5 17 Output: 14 4 15 3 9 18 7 16 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    22. 22. Level-order Traversal Queue: 5 17 Output: 14 4 15 3 9 18 7 16 20 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    23. 23. Level-order Traversal Queue: 17 Output: 14 4 15 3 9 18 7 16 20 5 14 4 9 7 3 5 15 18 16 20 17 http://ecomputernotes.com
    24. 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 http://ecomputernotes.com
    25. 25. Storing other Type of Data <ul><li>The examples of binary trees so far have been storing integer data in the tree node. </li></ul><ul><li>This is surely not a requirement. Any type of data can be stored in a tree node. </li></ul><ul><li>Here, for example, is the C++ code to build a tree with character strings. </li></ul>http://ecomputernotes.com
    26. 26. Binary Search Tree with Strings <ul><li>void wordTree() </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* root = new TreeNode<char>(); </li></ul><ul><li>static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, </li></ul><ul><li> &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, </li></ul><ul><li> &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; </li></ul><ul><li>root->setInfo( word[0] ); </li></ul><ul><li>for(i=1; word[i]; i++ ) </li></ul><ul><li>insert(root, word[i] ); </li></ul><ul><li>inorder( root ); cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    27. 27. Binary Search Tree with Strings <ul><li>void wordTree() </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* root = new TreeNode<char>(); </li></ul><ul><li>static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, </li></ul><ul><li> &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, </li></ul><ul><li> &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; </li></ul><ul><li>root->setInfo( word[0] ); </li></ul><ul><li>for(i=1; word[i]; i++ ) </li></ul><ul><li>insert(root, word[i] ); </li></ul><ul><li>inorder( root ); cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    28. 28. Binary Search Tree with Strings <ul><li>void wordTree() </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* root = new TreeNode<char>(); </li></ul><ul><li>static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, </li></ul><ul><li> &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, </li></ul><ul><li> &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; </li></ul><ul><li>root->setInfo( word[0] ); </li></ul><ul><li>for(i=1; word[i]; i++ ) </li></ul><ul><li>insert(root, word[i] ); </li></ul><ul><li>inorder( root ); cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    29. 29. Binary Search Tree with Strings <ul><li>void wordTree() </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* root = new TreeNode<char>(); </li></ul><ul><li>static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, </li></ul><ul><li> &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, </li></ul><ul><li> &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; </li></ul><ul><li>root->setInfo( word[0] ); </li></ul><ul><li>for(i=1; word[i]; i++ ) </li></ul><ul><li>insert(root, word[i] ); </li></ul><ul><li>inorder( root ); cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    30. 30. Binary Search Tree with Strings <ul><li>void wordTree() </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* root = new TreeNode<char>(); </li></ul><ul><li>static char* word[] = &quot;babble&quot;, &quot;fable&quot;, &quot;jacket&quot;, &quot;backup&quot;, &quot;eagle&quot;,&quot;daily&quot;,&quot;gain&quot;,&quot;bandit&quot;,&quot;abandon&quot;, </li></ul><ul><li> &quot;abash&quot;,&quot;accuse&quot;,&quot;economy&quot;,&quot;adhere&quot;,&quot;advise&quot;,&quot;cease&quot;, </li></ul><ul><li> &quot;debunk&quot;,&quot;feeder&quot;,&quot;genius&quot;,&quot;fetch&quot;,&quot;chain&quot;, NULL}; </li></ul><ul><li>root->setInfo( word[0] ); </li></ul><ul><li>for(i=1; word[i]; i++ ) </li></ul><ul><li>insert(root, word[i] ); </li></ul><ul><li>inorder( root ); cout << endl; </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    31. 31. Binary Search Tree with Strings <ul><li>void insert(TreeNode<char>* root, char* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* node = new TreeNode<char>(info); </li></ul><ul><li>TreeNode<char> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( strcmp(info, p->getInfo()) != 0 && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    32. 32. Binary Search Tree with Strings <ul><li>void insert(TreeNode<char>* root, char* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* node = new TreeNode<char>(info); </li></ul><ul><li>TreeNode<char> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( strcmp(info, p->getInfo()) != 0 && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    33. 33. Binary Search Tree with Strings <ul><li>void insert(TreeNode<char>* root, char* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* node = new TreeNode<char>(info); </li></ul><ul><li>TreeNode<char> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( strcmp(info, p->getInfo()) != 0 && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    34. 34. Binary Search Tree with Strings <ul><li>void insert(TreeNode<char>* root, char* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* node = new TreeNode<char>(info); </li></ul><ul><li>TreeNode<char> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( strcmp(info, p->getInfo()) != 0 && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    35. 35. Binary Search Tree with Strings <ul><li>void insert(TreeNode<char>* root, char* info) </li></ul><ul><li>{ </li></ul><ul><li>TreeNode<char>* node = new TreeNode<char>(info); </li></ul><ul><li>TreeNode<char> *p, *q; </li></ul><ul><li>p = q = root; </li></ul><ul><li>while( strcmp(info, p->getInfo()) != 0 && q != NULL ) </li></ul><ul><li>{ </li></ul><ul><li>p = q; </li></ul><ul><li>if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>q = p->getLeft(); </li></ul><ul><li>else </li></ul><ul><li>q = p->getRight(); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    36. 36. Binary Search Tree with Strings <ul><li>if( strcmp(info, p->getInfo()) == 0 ){ </li></ul><ul><li>cout << &quot;attempt to insert duplicate: &quot; << *info </li></ul><ul><li><< endl; </li></ul><ul><li>delete node; </li></ul><ul><li>} </li></ul><ul><li>else if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>p->setLeft( node ); </li></ul><ul><li>else </li></ul><ul><li>p->setRight( node ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    37. 37. Binary Search Tree with Strings <ul><li>if( strcmp(info, p->getInfo()) == 0 ){ </li></ul><ul><li>cout << &quot;attempt to insert duplicate: &quot; << *info </li></ul><ul><li><< endl; </li></ul><ul><li>delete node; </li></ul><ul><li>} </li></ul><ul><li>else if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>p->setLeft( node ); </li></ul><ul><li>else </li></ul><ul><li>p->setRight( node ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    38. 38. Binary Search Tree with Strings <ul><li>if( strcmp(info, p->getInfo()) == 0 ){ </li></ul><ul><li>cout << &quot;attempt to insert duplicate: &quot; << *info </li></ul><ul><li><< endl; </li></ul><ul><li>delete node; </li></ul><ul><li>} </li></ul><ul><li>else if( strcmp(info, p->getInfo()) < 0 ) </li></ul><ul><li>p->setLeft( node ); </li></ul><ul><li>else </li></ul><ul><li>p->setRight( node ); </li></ul><ul><li>} </li></ul>http://ecomputernotes.com 
    39. 39. Binary Search Tree with Strings <ul><li>Output: </li></ul><ul><ul><li>abandon </li></ul></ul><ul><ul><li>abash </li></ul></ul><ul><ul><li>accuse </li></ul></ul><ul><ul><li>adhere </li></ul></ul><ul><ul><li>advise </li></ul></ul><ul><ul><li>babble </li></ul></ul><ul><ul><li>backup </li></ul></ul><ul><ul><li>bandit </li></ul></ul><ul><ul><li>cease </li></ul></ul><ul><ul><li>chain </li></ul></ul><ul><ul><li>daily </li></ul></ul><ul><ul><li>debunk </li></ul></ul><ul><ul><li>eagle </li></ul></ul><ul><ul><li>economy </li></ul></ul><ul><ul><li>fable </li></ul></ul><ul><ul><li>feeder </li></ul></ul><ul><ul><li>fetch </li></ul></ul><ul><ul><li>gain </li></ul></ul><ul><ul><li>genius </li></ul></ul><ul><ul><li>jacket </li></ul></ul>http://ecomputernotes.com
    40. 40. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul>http://ecomputernotes.com
    41. 41. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul><ul><li>This should not come as a surprise if you consider how we built the BST. </li></ul>http://ecomputernotes.com
    42. 42. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul><ul><li>This should not come as a surprise if you consider how we built the BST. </li></ul><ul><li>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. </li></ul>http://ecomputernotes.com
    43. 43. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul><ul><li>This should not come as a surprise if you consider how we built the BST. </li></ul><ul><li>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. </li></ul><ul><li>Inorder prints the left subtree, then the node finally the right subtree. </li></ul>http://ecomputernotes.com
    44. 44. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul><ul><li>This should not come as a surprise if you consider how we built the BST. </li></ul><ul><li>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. </li></ul><ul><li>Inorder prints the left subtree, then the node finally the right subtree. </li></ul><ul><li>Building a BST and doing an inorder traversal leads to a sorting algorithm. </li></ul>http://ecomputernotes.com
    45. 45. Binary Search Tree with Strings <ul><li>abandon </li></ul><ul><li>abash </li></ul><ul><li>accuse </li></ul><ul><li>adhere </li></ul><ul><li>advise </li></ul><ul><li>babble </li></ul><ul><li>backup </li></ul><ul><li>bandit </li></ul><ul><li>cease </li></ul><ul><li>chain </li></ul><ul><li>daily </li></ul><ul><li>debunk </li></ul><ul><li>eagle </li></ul><ul><li>economy </li></ul><ul><li>fable </li></ul><ul><li>feeder </li></ul><ul><li>fetch </li></ul><ul><li>gain </li></ul><ul><li>genius </li></ul><ul><li>jacket </li></ul><ul><li>Notice that the words are sorted in increasing order when we traversed the tree in inorder manner. </li></ul><ul><li>This should not come as a surprise if you consider how we built the BST. </li></ul><ul><li>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. </li></ul><ul><li>Inorder prints the left subtree, then the node finally the right subtree. </li></ul><ul><li>Building a BST and doing an inorder traversal leads to a sorting algorithm. </li></ul>http://ecomputernotes.com
    46. 46. Deleting a node in BST <ul><li>As is common with many data structures, the hardest operation is deletion. </li></ul><ul><li>Once we have found the node to be deleted, we need to consider several possibilities. </li></ul><ul><li>If the node is a leaf , it can be deleted immediately. </li></ul>http://ecomputernotes.com
    47. 47. Deleting a node in BST <ul><li>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. </li></ul>http://ecomputernotes.com 6 2 4 3 1 8
    48. 48. Deleting a node in BST <ul><li>The inorder traversal order has to be maintained after the delete. </li></ul> http://ecomputernotes.com 6 2 4 3 1 8 6 2 4 3 1 8
    49. 49. Deleting a node in BST <ul><li>The inorder traversal order has to be maintained after the delete. </li></ul>  http://ecomputernotes.com 6 2 4 3 1 8 6 2 4 3 1 8 6 2 3 1 8
    50. 50. Deleting a node in BST <ul><li>The complicated case is when the node to be deleted has both left and right subtrees. </li></ul><ul><li>The strategy is to replace the data of this node with the smallest data of the right subtree and recursively delete that node. </li></ul>http://ecomputernotes.com
    51. 51. Deleting a node in BST <ul><li>Delete(2): locate inorder successor </li></ul>Inorder successor http://ecomputernotes.com 6 2 5 3 1 8 4
    52. 52. Deleting a node in BST <ul><li>Delete(2): locate inorder successor </li></ul>Inorder successor <ul><li>Inorder successor will be the left-most node in the right subtree of 2. </li></ul><ul><li>The inorder successor will not have a left child because if it did, that child would be the left-most node. </li></ul>http://ecomputernotes.com 6 2 5 3 1 8 4
    53. 53. Deleting a node in BST <ul><li>Delete(2): copy data from inorder successor </li></ul> http://ecomputernotes.com 6 2 5 3 1 8 4 6 3 5 3 1 8 4
    54. 54. Deleting a node in BST <ul><li>Delete(2): remove the inorder successor </li></ul>  http://ecomputernotes.com 6 2 5 3 1 8 4 6 3 5 3 1 8 4 6 3 5 3 1 8 4
    55. 55. Deleting a node in BST <ul><li>Delete(2) </li></ul>  http://ecomputernotes.com 6 3 5 4 1 8 6 3 5 3 1 8 4

    ×