cis22c/05-trees/main.cpp
Iurii Tatishchev 0bc2b9690f
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.
2024-05-03 14:00:39 -07:00

72 lines
1.2 KiB
C++

// BST ADT
// Name: Iurii Tatishchev
#include "BinarySearchTree.h"
#include <iostream>
#include <string>
using namespace std;
void buildBST(int n, BinarySearchTree<int> &);
void hDisplay(int &);
void vDisplay(int &);
void iDisplay(int &, int);
int main() {
BinarySearchTree<int> bst;
int n;
cout << "What is the number of nodes in the BST? " << endl;
cin >> n;
buildBST(n, bst);
cout << " Inorder: ";
bst.inOrder(hDisplay);
cout << endl;
cout << "Indented Tree:" << endl;
bst.printTree(iDisplay);
return 0;
}
/*
buildBST: builds a binary search tree
of integers
*/
void buildBST(int n, BinarySearchTree<int> &bst) {
int item;
while (n--) {
item = rand() % 30 + 10;
bst.insert(item);
}
}
/*
horizontal display: all items on one line
*/
void hDisplay(int &item) {
cout << item << " ";
}
/*
vertical display: one item per line
*/
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;
}