Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bintree.h"
- #include <cstdlib>
- #include <iostream>
- #include <queue>
- #include <cassert>
- using namespace std;
- using namespace main_savitch_10;
- int h;
- binary_tree_node<char>* create_tree ();
- // return: pointer root of binary search tree of integers
- template <typename T>
- int height(const binary_tree_node<T>* t){
- if (t == NULL)
- return 0;
- else
- {
- /* compute the height of each subtree */
- int lheight = height(t->left( ));
- int rheight = height(t->right( ));
- /* use the larger one */
- if (lheight > rheight)
- return(lheight + 1);
- else return(rheight + 1);
- }
- }
- template <typename T>
- void printGivenLevel(const binary_tree_node<T>* t, int level){
- if (t == NULL)
- return;
- if (level == 1)
- cout << t->data( ) << " ";
- else if (level > 1)
- {
- printGivenLevel(t->left( ), level-1);
- printGivenLevel(t->right( ), level-1);
- }
- }
- template <typename T>
- void display_complete_tree (const binary_tree_node<T>* t){
- h = height(t);
- int i = 0;
- for (i = 1; i <= h; i++){
- printGivenLevel(t, i);
- cout<<endl;
- }
- }
- int main ()
- {
- binary_tree_node<char>* t = create_tree ();
- display_complete_tree(t);
- return EXIT_SUCCESS;
- }
- binary_tree_node<char>* create_tree()
- {
- binary_tree_node<char>* t1 = new binary_tree_node<char>('A');
- binary_tree_node<char>* t2 = new binary_tree_node<char>('Q');
- binary_tree_node<char>* t3 = new binary_tree_node<char>('W');
- binary_tree_node<char>* t4 = new binary_tree_node<char>('E',t1,NULL);
- binary_tree_node<char>* t5 = new binary_tree_node<char>('H',t4,NULL);
- binary_tree_node<char>* t6 = new binary_tree_node<char>('P',NULL,t2);
- binary_tree_node<char>* t7 = new binary_tree_node<char>('T',t6,t3);
- binary_tree_node<char>* t8 = new binary_tree_node<char>('M',t5,t7);
- return t8;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement