-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDemo.cpp
More file actions
42 lines (37 loc) · 1.16 KB
/
TreeDemo.cpp
File metadata and controls
42 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* Demo program for the exercise on binary trees
*
* @author Erel Segal-Halevi
*
* @since 2019-02
*/
#include <iostream>
using std::cout, std::endl;
#include "Tree.hpp"
int main() {
try {
// constructs an empty tree:
ariel::Tree emptytree;
cout << "emptytree: size=" << emptytree.size() << endl; // should print 0
// constructs an ordered binary tree where:
// 5 is in the root;
// 3 is the root's left child;
// 7 is the root's right child.
ariel::Tree threetree;
threetree.insert(5);
threetree.insert(7);
threetree.insert(3);
cout << "threetree: size=" << threetree.size() << " root=" << threetree.root() << endl << " "; // size=3, root=5.
threetree.print();
cout << endl;
cout << threetree.size() // should print 3
<< threetree.parent(3) // should print 5
<< threetree.parent(7) // should print 5
<< threetree.left(5) // should print 3
<< threetree.right(5) // should print 7
<< endl;
threetree.insert(5); // should throw an exception, since 5 already exists.
} catch (...) {
cout << "Caught exception!" << endl;
}
}