fixed indented bst printing

This commit is contained in:
Tuhin Mondal 2024-06-20 04:38:25 -07:00
parent a74b3e2aca
commit def78cb76f
4 changed files with 14 additions and 13 deletions

View File

@ -28,7 +28,7 @@ public:
void postOrder(void visit(const T&)) const { _postorder(visit, root); } void postOrder(void visit(const T&)) const { _postorder(visit, root); }
void printIndented(void visit(const T&)) const { _printindented(visit, root, 0); } void printTree(void visit(const T&, int)) const { _printTree(visit, rootPtr, 1); }
// abstract functions to be implemented by derived class // abstract functions to be implemented by derived class
virtual void insert(const T& newData) = 0; virtual void insert(const T& newData) = 0;
@ -46,7 +46,7 @@ private:
void _postorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const; void _postorder(void visit(const T&), BinaryTreeNode<T>* nodePtr) const;
void _printindented(void visit(const T&, int), BinaryTreeNode<T>* nodePtr) const; void _printTree(void visit(const T&, int), BinaryTreeNode<T>* nodePtr, int level) const;
}; };
@ -95,13 +95,14 @@ void BinaryTree<T>::_postorder(void visit(const T&), BinaryTreeNode<T>* nodePtr)
} }
} }
template<typename T> template<class T>
void BinaryTree<T>::_printindented(void visit(const T&, int), BinaryTreeNode<T>* nodePtr) const { void BinaryTree<T>::_printTree(void visit(const T&, int), BinaryTreeNode<T>* nodePtr, int level) const
{
if (nodePtr) { if (nodePtr) {
T item = nodePtr->getItem(); ItemType item = nodePtr->getItem();
_printInnerNodes(visit, nodePtr->getLeftPtr()); visit(item, level);
if (nodePtr->getLeftPtr() || nodePtr->getRightPtr()) visit(item); _printTree(visit, nodePtr->getRightPtr(), level + 1);
_printInnerNodes(visit, nodePtr->getRightPtr()); _printTree(visit, nodePtr->getLeftPtr(), level + 1);
} }
} }

View File

@ -52,10 +52,10 @@ void display(const CPU &cpu) {
printTableFooter(widths); printTableFooter(widths);
} }
void iDisplay(const CPU &cpu, int level) { void iDisplay(const string &cpuId, int level) {
for (int i = 0; i < level; i++) for (int i = 0; i < level; i++)
cout << " "; cout << "..";
cout << cpu.cpuId << endl; cout << level << ")." << cpuId << endl;
} }
void rowDisplay(const CPU &cpu, const vector<int> &widths) { void rowDisplay(const CPU &cpu, const vector<int> &widths) {

2
CPU.h
View File

@ -74,7 +74,7 @@ public:
void display(const CPU &cpu); void display(const CPU &cpu);
void iDisplay(const CPU &cpu, int level); void iDisplay(const string &cpuId, int level);
void rowDisplay(const CPU &cpu, const std::vector<int> &widths); void rowDisplay(const CPU &cpu, const std::vector<int> &widths);

View File

@ -122,7 +122,7 @@ void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<strin
cout << "Longest collision path: " << cpuTable.getMaxCollisions() << std::endl; cout << "Longest collision path: " << cpuTable.getMaxCollisions() << std::endl;
break; break;
case 'P': // Print indented tree case 'P': // Print indented tree
throw std::logic_error("Not yet implemented: Print indented tree"); cpuTree.printTree(iDisplay);
break; break;
case 'Z': // Display names of team members case 'Z': // Display names of team members
printTeam(); printTeam();