1 / 15

Trees 4: Associative Binary Tree Structures

Trees 4: Associative Binary Tree Structures. Andy Wang Data Structures, Algorithms, and Generic Programming. Some Interesting Questions. How do you store a tree in a file? How do you use hash tables to implement trees?. Trees 4: Overview. Totally Ordered Trees [TOT] Binary Search Trees.

dakota
Download Presentation

Trees 4: Associative Binary Tree Structures

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. Trees 4: Associative Binary Tree Structures Andy Wang Data Structures, Algorithms, and Generic Programming

  2. Some Interesting Questions • How do you store a tree in a file? • How do you use hash tables to implement trees?

  3. Trees 4: Overview • Totally Ordered Trees [TOT] • Binary Search Trees

  4. 4 root 2 6 1 3 5 7 Totally Ordered Trees • Definition: A binary tree B is totally ordered iff: • There is an order relation <= defined for the vertices of B • For any vertex v of B and any descendant u of v.left_child, u <= v • For any vertex v of B and any descendent w of v.right_child, v <= w

  5. 4 root 2 6 1 3 5 7 Totally Ordered Trees • Consequences: • The smallest element in a totally ordered tree (TOT) is the “left-most” node • The largest element in a TOT is the “right-most” node • Inorder traversal of a TOT encounters nodes in increasing order

  6. Binary Search Revisited • Binary search—tree version • Assumes totally ordered binary tree • Begin at root node • Descend using comparison to make left/right decision • if (search_value < node_value) go to the left_child • else if (search_value > node_value) go to the right_child • else return true (success) • Until descending move is impossible • Return false (failure)

  7. Binary Search Revisited • Runtime <= descending path length <= depth of tree • If tree has enough branching, runtime <= O(log size)

  8. Binary Search Trees • Generate aContainers TUBST<T, P> and TMBST<T, P> • Unimodal or Multimodal versions • Derive from, adapt, or use pattern of, TBinaryTree<T> • Use TBinaryTreeInorderIterator<T> for TBST<T,P>::Iterator • Optional second parameter: predicate object determining order

  9. Binary Search Trees • Use binary search—tree version—for • TBST::Insert(t) • TBST::Remove() • TBST::Includes(t) • TBST::LowerBound(t) • TBST::UpperBound(t)

  10. A Binary Search Tree Class template <typename T, class P = TLessThan<T> > class TUBST : public TBinaryTree<T> { public: // terminology support typedef T value_type; typedef P predicate_type; typedef TBinaryTreeInorderIterator<T> Iterator; typedef TBinaryTreeNavigator<T> Navigator; // constructors TUBST(); TUBST(const TUBST<T, P>& S); // assignment TUBST<T, P>& operator=(const TUBST<T, P>& L); // BST operations Iterator Insert(const T& t); // overwrite if found, insert otherwise int Insert(Iterator& I, const T& t);

  11. A Binary Search Tree Class // locating elements Iterator LowerBound(const T& t) const; Iterator UpperBound(const T& t) const; Iterator Includes(const T& t) const; // use inherited functions for Remove, Clear, Empty, Size, iterators, // and outputs protected: P LessThan; private: int InsertRoot(const T& t); int InsertLeft(Navigator& N, const T& t); int InsertRight(Navigator& N, const T& t); };

  12. TUBST Implementation template <typenmae T, class P> TUBST<T, P>::TUBST() : LessThan() { } template <typename T, class P> TUBST<T, P>::TUBST(const TUBST<T, P>& b) : LessThan(b.LessThan) { } template <typename T, class P> TUBST<T, P>& TUBST<T, P>::operator=(const TUBST<T, P>& b) { if (this != &b) { TBinaryTree<T>::Clear(); root = TBinaryTree<T>::RClone(b.root); LessThan = b.LessThan; } return *this; }

  13. TUBST Implementation template <typenmae T, class P> int operator==(const TUBST<T, P>& B1, const TUBST<T, P>& B2) { TUBST<T, P>::Iterator I1(B1), I2(B2); while (I1.Valid() && I2.Valid()) { if (*(I1++) != *(I2++)) { return 0; } } if (I1.Valid() || I2.Valid()) { return 0; } return 1; } template <typename T, class P> int operator!=(const TUBST<T, P>& B1, const TUBST<T, P>& B2) { return !(B1 == B2); }

  14. TUBST Implementation template <typenmae T, class P> TUBST<T, P>::Iterator TUBST<T, P>::Includes(const T& t) const { TUBST<T, P>::Navigator N = Root(); while (N.Valid()) { if (LessThan(t, *N)) { ++N; } else if (LessThan(*N, t)) { N++; } else { return N; } } return End(); }

  15. Definition for TMBST template <typename T, class P = TLessThan<T> > class TMBST : public TUBST<T, P> { public: // terminology support typedef T value_type; typedef TBinaryTreeInorderIterator<T> Iterator; typedef TBinaryTreeNavigator<T> Navigator; // insert operations Iterator Insert(const T& t); // always insert int Insert(Iterator& I, const T& t); // assignment TMBST<T, P>& operator=(const TMBST<T, P>& L); // use other inherited functions };

More Related