Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "bintree.h"
- #include <cstdlib>
- #include <queue>
- using namespace std;
- using namespace main_savitch_10;
- template <typename T>
- void display_complete_tree (const binary_tree_node<T>* t) {
- if (t == NULL)
- return;
- queue<const binary_tree_node<T>* > q;
- q.push(t);
- while (q.empty() == false) {
- int size = q.size();
- while (size > 0) {
- const binary_tree_node<string> *node = q.front();
- cout << node->data() << " ";
- q.pop();
- if (node->left() != NULL) {
- q.push(node->left());
- } else if (node->left() == NULL && node->right() != NULL){
- cout << "NULL ";
- }
- if (node->right() != NULL) {
- q.push(node->right());
- } else if (node->right() == NULL && node->left() != NULL){
- cout << "NULL ";
- }
- size--;
- }
- cout << endl;
- }
- }
- binary_tree_node<string>* create_int_tree()
- {
- binary_tree_node<string>* t1 = new binary_tree_node<string>("14");
- binary_tree_node<string>* t2 = new binary_tree_node<string>("32");
- binary_tree_node<string>* t3 = new binary_tree_node<string>("6");
- binary_tree_node<string>* t4 = new binary_tree_node<string>("23", NULL, t1);
- binary_tree_node<string>* t5 = new binary_tree_node<string>("9", t2, t3);
- binary_tree_node<string>* t6 = new binary_tree_node<string>("17", t4, t5);
- return t6;
- }
- int main()
- {
- binary_tree_node<string>* t = create_int_tree();
- display_complete_tree(t);
- return EXIT_SUCCESS;
- }
- // returned: root of binary tree of integers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement