Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "BinaryTree.h"
- using namespace std;
- int ShowMenu();
- int main()
- {
- BinaryTree *tree = 0;
- int *treeElements;
- int choice = 0;
- for (;;)
- {
- choice = ShowMenu();
- if (choice != 0)
- {
- switch (choice)
- {
- case 1:
- {
- int elementsCount;
- if (tree != 0)
- {
- BinaryTree *delTree;
- delTree = tree;
- tree = 0;
- delete delTree;
- }
- cout << "How many elements?: ";
- cin >> elementsCount;
- treeElements = new int [elementsCount];
- for (int i=0; i<elementsCount;i++)
- {
- cout << "Enter element " << i+1 << ": ";
- cin >> treeElements[i];
- }
- tree = new BinaryTree();
- tree->Build(treeElements,elementsCount);
- break;
- }
- case 2:
- {
- if (tree != 0)
- {
- int startRow;
- cout << "Enter row number to print from (number between 1 -" << tree->GetRowsCount() << "): ";
- cin >> startRow;
- tree->Walk(startRow);
- }
- else cout << "There is no tree to walk" << endl;
- break;
- }
- default:
- cout << "Wrong command number" << endl;
- break;
- }
- }
- else
- {
- delete treeElements;
- delete tree;
- break;
- }
- }
- return 0;
- }
- int ShowMenu()
- {
- int choice = 0;
- cout << "1 - Create tree" << endl;
- cout << "2 - Print tree" << endl;
- cout << "0 - exit" << endl;
- cout << "Enter your choice: ";
- cin >> choice;
- if (choice == 0)
- {
- char confirm;
- cout << "Do you really want to exit the program? (y/n) ";
- cin >> confirm;
- return (confirm == 'n' || confirm == 'N') ? -1 : choice;
- }
- return choice;
- }
Advertisement
Add Comment
Please, Sign In to add comment