Binary Search Tree Implementation in C++: Class Declaration and Traversal Algorithms, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-okt
koofers-user-okt 🇺🇸

9 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CHAPTER 10 -- BINARY TREES
E. Binary Search Tree Implementation
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.
10
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Binary Search Tree Implementation in C++: Class Declaration and Traversal Algorithms and more Study notes Computer Science in PDF only on Docsity!

CHAPTER 10 -- BINARY TREES

E. Binary Search Tree Implementation

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.

Example: A tree as represented using our declaration

bstTree T;

Package Body (Carrano, Helman & Veroff, pp. 455-463)

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); }