Upload
dakota
View
28
Download
0
Embed Size (px)
DESCRIPTION
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. - PowerPoint PPT Presentation
Citation preview
Trees 4: Trees 4: Associative Associative Binary Tree Binary Tree StructuresStructuresAndy WangAndy Wang
Data Structures, Algorithms, Data Structures, Algorithms, and Generic Programmingand Generic Programming
Some Interesting Some Interesting QuestionsQuestions
How do you store a tree in a file?How do you store a tree in a file? How do you use hash tables to How do you use hash tables to
implement trees?implement trees?
Trees 4: OverviewTrees 4: Overview
Totally Ordered Trees [TOT]Totally Ordered Trees [TOT] Binary Search TreesBinary Search Trees
Totally Ordered TreesTotally Ordered Trees
Definition: A binary tree B is Definition: A binary tree B is totally totally orderedordered iff: iff: There is an order relation <= defined There is an order relation <= defined
for the vertices of Bfor the vertices of B For any vertex v of B and any For any vertex v of B and any
descendant u of v.left_child, u <= vdescendant u of v.left_child, u <= v For any vertex v of B and any For any vertex v of B and any
descendent w of v.right_child, v <= wdescendent w of v.right_child, v <= w
4
2 6
1 3 75
root
Totally Ordered TreesTotally Ordered Trees
Consequences:Consequences: The smallest element in a totally The smallest element in a totally
ordered tree (TOT) is the “left-most” ordered tree (TOT) is the “left-most” nodenode
The largest element in a TOT is the The largest element in a TOT is the “right-most” node“right-most” node
Inorder traversal of a TOT encounters Inorder traversal of a TOT encounters nodes in increasing ordernodes in increasing order
4
2 6
1 3 75
root
Binary Search RevisitedBinary Search Revisited
Binary search—tree versionBinary search—tree version Assumes totally ordered binary treeAssumes totally ordered binary tree
Begin at root nodeBegin at root node Descend using comparison to make left/right Descend using comparison to make left/right
decisiondecision if (search_value < node_value) go to the left_childif (search_value < node_value) go to the left_child else if (search_value > node_value) go to the else if (search_value > node_value) go to the
right_childright_child else return true (success)else return true (success)
Until descending move is impossibleUntil descending move is impossible Return false (failure)Return false (failure)
Binary Search RevisitedBinary Search Revisited
Runtime <= descending path length Runtime <= descending path length <= depth of tree<= depth of tree
If tree has enough branching, If tree has enough branching, runtime <= O(log size)runtime <= O(log size)
Binary Search TreesBinary Search Trees
Generate aContainers TUBST<T, P> Generate aContainers TUBST<T, P> and TMBST<T, P>and TMBST<T, P>
Unimodal or Multimodal versionsUnimodal or Multimodal versions Derive from, adapt, or use pattern Derive from, adapt, or use pattern
of, TBinaryTree<T>of, TBinaryTree<T> Use TBinaryTreeInorderIterator<T> Use TBinaryTreeInorderIterator<T>
for TBST<T,P>::Iteratorfor TBST<T,P>::Iterator Optional second parameter: Optional second parameter:
predicate object determining orderpredicate object determining order
Binary Search TreesBinary Search Trees
Use binary search—tree version—for Use binary search—tree version—for TBST::Insert(t)TBST::Insert(t) TBST::Remove()TBST::Remove() TBST::Includes(t)TBST::Includes(t) TBST::LowerBound(t)TBST::LowerBound(t) TBST::UpperBound(t)TBST::UpperBound(t)
A Binary Search Tree A Binary Search Tree ClassClass
template <typename T, class P = TLessThan<T> >template <typename T, class P = TLessThan<T> >
class TUBST : public TBinaryTree<T> {class TUBST : public TBinaryTree<T> {
public:public:
// terminology support// terminology support
typedef T value_type;typedef T value_type;
typedef P predicate_type;typedef P predicate_type;
typedef TBinaryTreeInorderIterator<T> Iterator;typedef TBinaryTreeInorderIterator<T> Iterator;
typedef TBinaryTreeNavigator<T> Navigator;typedef TBinaryTreeNavigator<T> Navigator;
// constructors// constructors
TUBST();TUBST();
TUBST(const TUBST<T, P>& S);TUBST(const TUBST<T, P>& S);
// assignment// assignment
TUBST<T, P>& operator=(const TUBST<T, P>& L);TUBST<T, P>& operator=(const TUBST<T, P>& L);
// BST operations// BST operations
Iterator Insert(const T& t); // overwrite if found, insert otherwiseIterator Insert(const T& t); // overwrite if found, insert otherwise
int Insert(Iterator& I, const T& t);int Insert(Iterator& I, const T& t);
A Binary Search Tree A Binary Search Tree ClassClass
// locating elements// locating elements
Iterator LowerBound(const T& t) const;Iterator LowerBound(const T& t) const;
Iterator UpperBound(const T& t) const;Iterator UpperBound(const T& t) const;
Iterator Includes(const T& t) const;Iterator Includes(const T& t) const;
// use inherited functions for Remove, Clear, Empty, Size, iterators, // use inherited functions for Remove, Clear, Empty, Size, iterators,
// and outputs// and outputs
protected:protected:
P LessThan;P LessThan;
private:private:
int InsertRoot(const T& t);int InsertRoot(const T& t);
int InsertLeft(Navigator& N, const T& t);int InsertLeft(Navigator& N, const T& t);
int InsertRight(Navigator& N, const T& t);int InsertRight(Navigator& N, const T& t);
};};
TUBST ImplementationTUBST Implementationtemplate <typenmae T, class P>template <typenmae T, class P>
TUBST<T, P>::TUBST() : LessThan() { }TUBST<T, P>::TUBST() : LessThan() { }
template <typename T, class P>template <typename T, class P>
TUBST<T, P>::TUBST(const TUBST<T, P>& b) : LessThan(b.LessThan) { }TUBST<T, P>::TUBST(const TUBST<T, P>& b) : LessThan(b.LessThan) { }
template <typename T, class P>template <typename T, class P>
TUBST<T, P>& TUBST<T, P>::operator=(const TUBST<T, P>& b) {TUBST<T, P>& TUBST<T, P>::operator=(const TUBST<T, P>& b) {
if (this != &b) {if (this != &b) {
TBinaryTree<T>::Clear();TBinaryTree<T>::Clear();
root = TBinaryTree<T>::RClone(b.root);root = TBinaryTree<T>::RClone(b.root);
LessThan = b.LessThan;LessThan = b.LessThan;
}}
return *this;return *this;
}}
TUBST ImplementationTUBST Implementationtemplate <typenmae T, class P>template <typenmae T, class P>
int operator==(const TUBST<T, P>& B1, const TUBST<T, P>& B2) {int operator==(const TUBST<T, P>& B1, const TUBST<T, P>& B2) {
TUBST<T, P>::Iterator I1(B1), I2(B2);TUBST<T, P>::Iterator I1(B1), I2(B2);
while (I1.Valid() && I2.Valid()) {while (I1.Valid() && I2.Valid()) {
if (*(I1++) != *(I2++)) {if (*(I1++) != *(I2++)) {
return 0;return 0;
}}
}}
if (I1.Valid() || I2.Valid()) {if (I1.Valid() || I2.Valid()) {
return 0;return 0;
}}
return 1;return 1;
}}
template <typename T, class P>template <typename T, class P>
int operator!=(const TUBST<T, P>& B1, const TUBST<T, P>& B2) {int operator!=(const TUBST<T, P>& B1, const TUBST<T, P>& B2) {
return !(B1 == B2);return !(B1 == B2);
}}
TUBST ImplementationTUBST Implementationtemplate <typenmae T, class P>template <typenmae T, class P>
TUBST<T, P>::Iterator TUBST<T, P>::Includes(const T& t) const {TUBST<T, P>::Iterator TUBST<T, P>::Includes(const T& t) const {
TUBST<T, P>::Navigator N = Root();TUBST<T, P>::Navigator N = Root();
while (N.Valid()) {while (N.Valid()) {
if (LessThan(t, *N)) {if (LessThan(t, *N)) {
++N;++N;
} else if (LessThan(*N, t)) {} else if (LessThan(*N, t)) {
N++;N++;
} else {} else {
return N;return N;
}}
}}
return End();return End();
}}
Definition for TMBSTDefinition for TMBSTtemplate <typename T, class P = TLessThan<T> >template <typename T, class P = TLessThan<T> >
class TMBST : public TUBST<T, P> {class TMBST : public TUBST<T, P> {
public:public:
// terminology support// terminology support
typedef T value_type;typedef T value_type;
typedef TBinaryTreeInorderIterator<T> Iterator;typedef TBinaryTreeInorderIterator<T> Iterator;
typedef TBinaryTreeNavigator<T> Navigator;typedef TBinaryTreeNavigator<T> Navigator;
// insert operations// insert operations
Iterator Insert(const T& t); // always insertIterator Insert(const T& t); // always insert
int Insert(Iterator& I, const T& t);int Insert(Iterator& I, const T& t);
// assignment// assignment
TMBST<T, P>& operator=(const TMBST<T, P>& L);TMBST<T, P>& operator=(const TMBST<T, P>& L);
// use other inherited functions// use other inherited functions
};};