150 likes | 300 Views
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.
E N D
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
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
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
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)
Binary Search Revisited • Runtime <= descending path length <= depth of tree • If tree has enough branching, runtime <= O(log size)
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
Binary Search Trees • Use binary search—tree version—for • TBST::Insert(t) • TBST::Remove() • TBST::Includes(t) • TBST::LowerBound(t) • TBST::UpperBound(t)
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);
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); };
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; }
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); }
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(); }
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 };