





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The class declaration and traversal algorithms for a binary search tree implemented in c++. The definition of a tree node, the binary search tree class, and the traversal functions: preordertraverse, inordertraverse, and postordertraverse.
Typology: Study notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






The following C++ statements describe the data in a tree node (from Carrano, Helmann & Veroff, p. 470): class itemClass { public: // … keyType Key() const; // returns search key private: keyType SearchKey; // … and possibly other data members }; A pointer based implementation of the ADT binary search tree (from Carrano, Helmann & Veroff, pp. 487-495): typedef desired-type-of-search-key keyType; #include "Data.h" // definition of itemClass typedef itemClass treeItemType; // See page 464 in the textbook. typedef void (functionType)(treeItemType& AnItem);* struct treeNode; // forward reference typedef treeNode* ptrType; // pointer to node struct treeNode { treeItemType Item; ptrType LChildPtr, RChildPtr; // constructor treeNode(const treeItemType& NodeItem, ptrType L, ptrType R ) : Item(NodeItem), LChildPtr(L), RChildPtr(R) {}; }; Note: The declaration creates recursively defined left and right subtrees stored by key value. Data is stored in each node.
bstTree T;
void bstClass::PreorderTraverse(functionType Visit) { Preorder(Root, Visit); } void bstClass::Preorder (ptrType TreePtr, functionType Visit) { if (TreePtr != NULL) { Visit(TreePtr->Item); Preorder(TreePtr->LChildPtr, Visit); Preorder(TreePtr->RChildPtr, Visit); } } void bstClass::InorderTraverse(functionType Visit) { Inorder(Root, Visit); } void bstClass::Inorder (ptrType TreePtr, functionType Visit) { if (TreePtr != NULL) { Inorder(TreePtr->LChildPtr, Visit); Visit(TreePtr->Item); Inorder(TreePtr->RChildPtr, Visit); } } void bstClass::PostorderTraverse(functionType Visit) { Postorder(Root, Visit); } void bstClass::Postorder (ptrType TreePtr, functionType Visit) { if (TreePtr != NULL) { Postorder(TreePtr->LChildPtr, Visit); Postorder(TreePtr->RChildPtr, Visit); Visit(TreePtr->Item); }
void bstClass::SearchTreeRetrieve(keyType SearchKey, treeItemType& TreeItem, bool& Success) const { RetrieveItem(Root, SearchKey, TreeItem, Success); } void bstClass::RetrieveItem(ptrType TreePtr, keyType SearchKey, TreeItemType& TreeItem, bool& Success) const { if (TreePtr == NULL) Success = false; // empty tree else if (SearchKey == TreePtr->Item.Key()) { // item is in the root of some subtree TreeItem = TreePtr->Item; Success = true; } else if (SearchKey < TreePtr->Item.Key()) // search the left subtree RetrieveItem(TreePtr->LChildPtr, SearchKey, TreeItem, Success); else // search the right subtree RetrieveItem(TreePtr->RChildPtr, SearchKey, TreeItem, Success); }
void bstClass::InsertItem(ptrType& TreePtr, const treeItemType& NewItem, bool& Success) { if (TreePtr == NULL) { // position of insertion found; insert after leaf // create a new node TreePtr = new treeNode(NewItem, NULL, NULL); // was allocation successful? Success = bool(TreePtr !=NULL); } // else search for the insertion position else if (NewItem.Key() < TreePtr_>Item.Key()) // search the left subtree InsertItem(TreePtr->LChildPtr, NewItem, Success); else // search the right subtree InsertItem(TreePtr->RChildPtr, NewItem, Success); } void bstClass::DeleteItem(ptrType& TreePtr, keyType SearchKey, bool& Success) { if (TreePtr == NULL) Success = false; else if (SearchKey == TreePtr->Item.Key()) { // item is in the root of some subtree DeleteNodeItem(TreePtr); // delete the item Success = true; } else if (SearchKey < TreePtr->Item.Key()) // search the left subtree DeleteItem(TreePtr->LChildPtr, SearchKey, Success); else // search the right subtree DeleteItem(TreePtr->RChildPtr, SearchKey, Success); }