Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNCA CSCI 363 24 September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.

Similar presentations


Presentation on theme: "UNCA CSCI 363 24 September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright."— Presentation transcript:

1 UNCA CSCI 363 24 September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright © 2000, 2001

2 Binary Search Trees BST Property: All elements stored in the left subtree of a node with value K have values = K.

3 BST ADT(1) // BST implementation for the Dictionary ADT template <class Key, class Elem, class KEComp, class EEComp> class BST : public Dictionary<Key, Elem, KEComp, EEComp> { private: BinNode * root; // Root of the BST int nodecount; // Number of nodes void clearhelp(BinNode *); BinNode * inserthelp(BinNode *, const Elem&); BinNode * deletemin(BinNode *,BinNode *&); BinNode * removehelp(BinNode *, const Key&, BinNode *&); bool findhelp(BinNode *, const Key&, Elem&) const; void printhelp(BinNode *, int) const;

4 BST ADT(2) public: BST() { root = NULL; nodecount = 0; } ~BST() { clearhelp(root); } void clear() { clearhelp(root); root = NULL; nodecount = 0; } bool insert(const Elem& e) { root = inserthelp(root, e); nodecount++; return true; } bool remove(const Key& K, Elem& e) { BinNode * t = NULL; root = removehelp(root, K, t); if (t == NULL) return false; e = t->val(); nodecount--; delete t; return true; }

5 BST ADT(3) bool removeAny(Elem& e) { // Delete min value if (root == NULL) return false; // Empty BinNode * t; root = deletemin(root, t); e = t->val(); delete t; nodecount--; return true; } bool find(const Key& K, Elem& e) const { return findhelp(root, K, e); } int size() { return nodecount; } void print() const { if (root == NULL) cout << "The BST is empty.\n"; else printhelp(root, 0); }

6 BST Search template <class Key, class Elem, class KEComp, class EEComp> bool BST :: findhelp(BinNode * subroot, const Key& K, Elem& e) const { if (subroot == NULL) return false; else if (KEComp::lt(K, subroot->val())) return findhelp(subroot->left(), K, e); else if (KEComp::gt(K, subroot->val())) return findhelp(subroot->right(), K, e); else { e = subroot->val(); return true; } }

7 BST Insert

8 Remove Minimum Value template <class Key, class Elem, class KEComp, class EEComp> BinNode * BST<Key, Elem, KEComp, EEComp>:: deletemin(BinNode * subroot, BinNode *& min) { if (subroot->left() == NULL) { min = subroot; return subroot->right(); } else { // Continue left subroot->setLeft( deletemin(subroot->left(), min)); return subroot; }

9 BST Remove

10 Cost of BST Operations Find: Insert: Delete: If the tree is not well-balanced, the cost can be very high.

11 What’s a BST for? Performance in unreliable –Need a way to maintain balance Branching factor of two is much too small –Branches in the 100’s would be better Awful if data is almost sorted –Though you could rebalance

12 Heaps Heap: Complete binary tree with the heap property: Min-heap: All values less than child values. Max-heap: All values greater than child values. The values are partially ordered. Heap representation: Normally the array- based complete binary tree representation.

13 Good use for a heap While nodes remain –Select and remove the smallest value –Update heap based on this selection

14 Binary Search Tree BST class (Fig 5.14) BST class test


Download ppt "UNCA CSCI 363 24 September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright."

Similar presentations


Ads by Google