8.9 Lab: BT <--- BST (Indented Tree)

Write a variation of one of the Depth-First Traversal Functions named printTree that displays the indented tree, including the level numbers.
This commit is contained in:
Iurii Tatishchev 2024-05-03 14:00:39 -07:00
parent 8e55246e9c
commit 0bc2b9690f
Signed by: CaZzzer
GPG Key ID: 28BE602058C08557
2 changed files with 31 additions and 20 deletions

View File

@ -40,7 +40,8 @@ public:
void inOrder(void visit(ItemType &)) const { _inorder(visit, rootPtr); } void inOrder(void visit(ItemType &)) const { _inorder(visit, rootPtr); }
void postOrder(void visit(ItemType &)) const { _postorder(visit, rootPtr); } void postOrder(void visit(ItemType &)) const { _postorder(visit, rootPtr); }
// void printTree(void visit(ItemType &, int)) const{_printTree(visit, rootPtr, 1);}
void printTree(void visit(ItemType &, int)) const { _printTree(visit, rootPtr, 1); }
// abstract functions to be implemented by derived class // abstract functions to be implemented by derived class
virtual bool insert(const ItemType &newData) = 0; virtual bool insert(const ItemType &newData) = 0;
@ -57,7 +58,8 @@ private:
void _inorder(void visit(ItemType &), BinaryNode<ItemType> *nodePtr) const; void _inorder(void visit(ItemType &), BinaryNode<ItemType> *nodePtr) const;
void _postorder(void visit(ItemType &), BinaryNode<ItemType> *nodePtr) const; void _postorder(void visit(ItemType &), BinaryNode<ItemType> *nodePtr) const;
// void _printTree(void visit(ItemType &, int), BinaryNode<ItemType>* nodePtr, int level) const;
void _printTree(void visit(ItemType &, int), BinaryNode<ItemType> *nodePtr, int level) const;
}; };
@ -108,4 +110,15 @@ void BinaryTree<ItemType>::_postorder(void visit(ItemType &), BinaryNode<ItemTyp
visit(item); visit(item);
} }
// Prints tree as an indented list
template<class ItemType>
void BinaryTree<ItemType>::_printTree(void visit(ItemType &, int), BinaryNode<ItemType> *nodePtr, int level) const {
if (nodePtr == nullptr) return;
ItemType item = nodePtr->getItem();
visit(item, level);
_printTree(visit, nodePtr->getRightPtr(), level + 1);
_printTree(visit, nodePtr->getLeftPtr(), level + 1);
}
#endif #endif

View File

@ -1,5 +1,4 @@
// BST ADT // BST ADT
// Smallest/Largest
// Name: Iurii Tatishchev // Name: Iurii Tatishchev
#include "BinarySearchTree.h" #include "BinarySearchTree.h"
@ -14,35 +13,24 @@ void hDisplay(int &);
void vDisplay(int &); void vDisplay(int &);
void iDisplay(int &, int);
int main() { int main() {
BinarySearchTree<int> bst; BinarySearchTree<int> bst;
int n; int n;
char option;
cout << "What is the number of nodes in the BST? " << endl; cout << "What is the number of nodes in the BST? " << endl;
cin >> n; cin >> n;
cout << "Find Smallest or Largest[S/L]? " << endl;
cin >> option;
buildBST(n, bst); buildBST(n, bst);
if (n < 15) { cout << " Inorder: ";
cout << " Inorder: "; bst.inOrder(hDisplay);
bst.inOrder(hDisplay); cout << endl;
cout << endl;
}
if (option == 'S' || option == 's') {
int minVal;
bst.findSmallest(minVal);
cout << "Smallest: " << minVal << endl;
} else if (option == 'L' || option == 'l') {
int maxVal;
bst.findLargest(maxVal);
cout << "Largest: " << maxVal << endl;
}
cout << "Indented Tree:" << endl;
bst.printTree(iDisplay);
return 0; return 0;
} }
@ -72,3 +60,13 @@ void hDisplay(int &item) {
void vDisplay(int &item) { void vDisplay(int &item) {
cout << item << endl; cout << item << endl;
} }
/*
indented tree display: one item per line, including the level number
*/
void iDisplay(int &item, int level) {
for (int i = 1; i < level; i++)
cout << "..";
cout << level << "). " << item << endl;
}