1 / 13

CSE 3358 Note Set 12

CSE 3358 Note Set 12. Data Structures and Algorithms. Exam 1 Next Wednesday 9/24/08. Deletion by Copying. 13. 25. 20. 31. 23. 39. 21. 24. template<class T> void BST<T>:: deleteByCopying ( BSTNode <T>*& node) { BSTNode <T> *previous, * tmp = node;

salali
Download Presentation

CSE 3358 Note Set 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 3358 Note Set 12 Data Structures and Algorithms

  2. Exam 1 Next Wednesday 9/24/08

  3. Deletion by Copying 13 25 20 31 23 39 21 24

  4. template<class T> void BST<T>::deleteByCopying(BSTNode<T>*& node) { BSTNode<T> *previous, *tmp = node; if (node->right == 0) node = node->left; else if (node->left == 0) node = node->right; else { tmp = node->left; previous = node; while (tmp->right != 0) { previous = tmp; tmp = tmp->right; } node->key = tmp->key; if (previous == node) previous->left = tmp->left; else previous->right = tmp->left; } delete tmp; }

  5. Some Different Cases //// see prev slides else { tmp = node->left; previous = node; while (tmp->right != 0) { previous = tmp; tmp = tmp->right; } node->key = tmp->key; if (previous == node) previous->left = tmp->left; else previous->right = tmp->left; } 13 Delete 25 25 20 31 16

  6. Some Different Cases //// see prev slides else { tmp = node->left; previous = node; while (tmp->right != 0) { previous = tmp; tmp = tmp->right; } node->key = tmp->key; if (previous == node) previous->left = tmp->left; else previous->right = tmp->left; } 13 Delete 25 25 20 31 23 22

  7. Balancing Act • Perfectly Balanced Tree: All leaves are to be found on one level or two levels • For perfectly balanced tree, height = ceil(lg(n)) • Example: 10000 nodes height = ceil(lg(10000)) = ceil(13.289) = 14 • This means, for a perfectly balanced tree, at most 14 nodes have to be checked to locate a particular element

  8. How to Balance • Depends on when tree is formed in relation to when searches are performed • If all data is present before tree is formed, • Store in array • Sort using efficient sorting algo • Used binary search type algo to create tree

  9. Example • Initial Data Set: • 5 1 9 8 7 0 2 3 4 6 • Sorted: • 0 1 2 3 4 5 6 7 8 9 4 Middle element becomes root Then recursively process right subtree then left subtree

  10. Example • Sorted: • 0 1 2 3 4 5 6 7 8 9 4 1

  11. Binary Search Balance template <class T> void BST<T>::balance (T data[], int first, int last) { if(first <= last) { int middle = (first + last)/2; insert(data[middle]); balance(data, first, middle – 1); balance(data, middle + 1, last); } }

  12. Discussion • All data must be present to create the tree • Subsequent inserts can lead to unbalancing tree • Best case sorting of array is n*lg(n) • Can produce array/list from tree w/ inorder traversal • Space needed? • Then recreate the tree w/ balance() method

  13. ?

More Related