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:
parent
8e55246e9c
commit
0bc2b9690f
@ -40,7 +40,8 @@ public:
|
||||
void inOrder(void visit(ItemType &)) const { _inorder(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
|
||||
virtual bool insert(const ItemType &newData) = 0;
|
||||
@ -57,7 +58,8 @@ private:
|
||||
void _inorder(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);
|
||||
}
|
||||
|
||||
// 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
|
||||
|
@ -1,5 +1,4 @@
|
||||
// BST ADT
|
||||
// Smallest/Largest
|
||||
// Name: Iurii Tatishchev
|
||||
|
||||
#include "BinarySearchTree.h"
|
||||
@ -14,35 +13,24 @@ void hDisplay(int &);
|
||||
|
||||
void vDisplay(int &);
|
||||
|
||||
void iDisplay(int &, int);
|
||||
|
||||
int main() {
|
||||
BinarySearchTree<int> bst;
|
||||
|
||||
int n;
|
||||
char option;
|
||||
|
||||
cout << "What is the number of nodes in the BST? " << endl;
|
||||
cin >> n;
|
||||
cout << "Find Smallest or Largest[S/L]? " << endl;
|
||||
cin >> option;
|
||||
|
||||
buildBST(n, bst);
|
||||
|
||||
if (n < 15) {
|
||||
cout << " Inorder: ";
|
||||
bst.inOrder(hDisplay);
|
||||
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 << " Inorder: ";
|
||||
bst.inOrder(hDisplay);
|
||||
cout << endl;
|
||||
|
||||
cout << "Indented Tree:" << endl;
|
||||
bst.printTree(iDisplay);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -72,3 +60,13 @@ void hDisplay(int &item) {
|
||||
void vDisplay(int &item) {
|
||||
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;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user