Question: Hello, can you please do the preorder for Thread Bin Tree? If you want more info, please do so. C++ only. ThreadBinTree.h #ifndef THREADBINTREE_H #define
Hello, can you please do the preorder for Thread Bin Tree? If you want more info, please do so. C++ only.
ThreadBinTree.h
#ifndef THREADBINTREE_H #define THREADBINTREE_H
#include
#include "BinSearchTree.h" #include "threadBinNode.h"
template < typename BaseData > class BinThreadTree : public BinSearchTree {
public: BinThreadTree ( bool (*precedes)(const BaseData& x, const BaseData& y) ); // BinSearchTree (const BinSearchTree &initBinSTree); void preorder(void (*processNode)(BaseData &item)); void inorder(void (*processNode)(BaseData &item)); virtual bool inOrderAdd (BaseData item); virtual bool preOrderAdd (BaseData item);
protected:
private: threadBinNode *root;
};
#include "ThreadBinTree.t"
#endif
ThreadBinTree.t
#ifndef THREADBINTREE_T #define THREADBINTREE_T
#include
#ifndef leftChild #define leftChild firstChild #endif
#ifndef rightChild #define rightChild sibling #endif
template BinThreadTree:: BinThreadTree ( bool (*precedes)(const BaseData& x, const BaseData& y) ) : BinSearchTree ( BinSearchTree::precedes ) {
if ( GenTree::root == 0 ) delete GenTree::root;
BinThreadTree::root = new threadBinNode; BinThreadTree::root -> leftChild = BinThreadTree::root; BinThreadTree::root -> rightChild = BinThreadTree::root; BinThreadTree::root -> leftThread = true; BinThreadTree::root -> rightThread = false;
}
/*
template BinSearchTree::BinSearchTree (const BinSearchTree &initBinSTree) { precedes=initBinSTree.precedes; }
*/
template bool BinThreadTree::inOrderAdd( BaseData item) {
threadBinNode *q, *p, *parentq; bool left, done; p = new threadBinNode; p -> info = item;
// Prepare for the while test by following the left child pointer // from the dummy header
parentq = BinThreadTree::root; q = static_cast< threadBinNode* > ( BinThreadTree::root -> leftChild ) ;
left = true; done = parentq -> leftThread;
while ( !done )
// Now allow q and its parent to travel down an appropriate branch until // an insertion spot is found // if (precedes( item, q -> info) ) if ( item <= q -> info ) { parentq = static_cast< threadBinNode* >(q); q = static_cast< threadBinNode* > (q -> leftChild ) ; left = true; done = parentq -> leftThread; } else {
parentq = static_cast< threadBinNode* >(q); q = static_cast< threadBinNode* > ( q -> rightChild ) ; left = false; done = parentq -> rightThread; }
// Now insert p as the left or right child of parentq p -> leftThread = true; p -> rightThread = true; if (left) { p -> leftChild = static_cast< threadBinNode* >( parentq -> leftChild); p -> rightChild =static_cast< threadBinNode* >(parentq); parentq -> leftChild = static_cast< threadBinNode* >(p); parentq -> leftThread = false; } else { p -> rightChild = static_cast< threadBinNode* >(parentq -> rightChild); p -> leftChild = static_cast< threadBinNode* >(parentq);
parentq -> rightChild = static_cast< threadBinNode* >(p); parentq -> rightThread = false; }
return true ; }
template void BinThreadTree::inorder(void (*processNode)(BaseData &item)) {
threadBinNode *p; p = BinThreadTree::root; do { if (p->rightThread) p = static_cast< threadBinNode* >(p->rightChild); else { p = static_cast< threadBinNode* >(p->rightChild); while (!p->leftThread) p = static_cast< threadBinNode* >(p->leftChild); } if (p != BinThreadTree::root ) processNode(p->info); } while (p != root); }
template bool BinThreadTree::preOrderAdd( BaseData item) {
threadBinNode* p; p = BinThreadTree::root->leftThread;
BaseData sum = 0.0; do { if (p != BinThreadTree::root) sum = sum + item; if (!p->firstChild) p = p->firstChild; else p = p->sibling; } while (p != BinThreadTree::root);
return true ; }
template void BinThreadTree::preorder(void (*processNode)(BaseData &item)) {
threadBinNode *p; p = static_cast< threadBinNode* > ( BinThreadTree::root -> leftChild ) ;
do { if ( p != BinThreadTree::root ) processNode( p->info ); if (!p -> leftThread ) p = static_cast< threadBinNode* >(p -> leftChild); else p = static_cast< threadBinNode* >(p->rightChild);
} while (p != BinThreadTree::root );
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
