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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <new>
//------------------------------------using----------------------------------//
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using std::nothrow;
//------------------------------------struct---------------------------------//
struct Node {
int _data;
struct Node *_left,*_right;
};
//------------------------------------prototype------------------------------//
struct Node *build_tree();
void insert_into_tree(int num,struct Node *&root);
const Node * min_depth_leaf(const struct Node* root);
void closest_leaf_to_root(const Node *root,int counter,int temp_counter,const Node *leaf);
void check_allocated(const struct Node *root);
void free_tree(struct Node *root);
//------------------------------------main-----------------------------------//
int main()
{
struct Node *root;
root=build_tree();
free_tree(root);
return EXIT_SUCCESS;
}
//----------------------------------function---------------------------------//
struct Node * build_tree()
{
struct Node *root=NULL;
int num;
cin>>num;
while(num!=0) //change to eof
{
insert_into_tree(num,root);
cin >>num;
}
return root;
}
//----------------------------------function---------------------------------//
void insert_into_tree(int num,struct Node *&root)
{
if (root==NULL)
{
root=new (nothrow) struct Node;
check_allocated(root);
root->_data=num;
root->_left=root->_right=NULL;
}
else if(num<=root->_data)
insert_into_tree(num,root->_left);
else
insert_into_tree(num,root->_right);
}
//----------------------------------function---------------------------------//
const Node *min_depth_leaf(const struct Node*root)
{
const Node *leaf;
int counter=0;
int temp_counter=-1;
closest_leaf_to_root(root,counter,temp_counter,leaf);
return leaf;
}
//----------------------------------function---------------------------------//
void closest_leaf_to_root(const Node *root,int counter,int temp_counter,
const Node *leaf)
{
if(leaf==NULL)
return;
}
//----------------------------------function---------------------------------//
void free_tree(struct Node *root)
{
if (root!=NULL)
{
free_tree(root->_left);
free_tree(root->_right);
delete root;
}
}
//----------------------------------function---------------------------------//
void check_allocated(const struct Node *root)
{
if(root==NULL)
{
cerr<<"cannot allocate meomry"<<endl;
exit(EXIT_FAILURE);
}
}
|