Right-heavy tree-デカルトの木

4072 ワード

 、Problem Description:

    A right-heavy tree is a binary tree where the value of a node is greater than or equal to the values of the nodes in its left subtree and less than the values of the nodes in its right subtree. A right-heavy tree could be empty.
    Write a program that will create a right-heavy tree from a given input sequence and then traverse the tree and printing the value of the node each time a node is visited using inorder, preorder and postorder traversal.
    The program should create the nodes in the tree dynamically. Thus, basically the tree can be of any size limited only by the amount of memory available in the computer.
Input:
    The first number in the input indicates the number of nodes in the tree. Then, the input is followed by the integers comprising the values of the nodes of the tree. (the first node as the root node)
Output:
    The output will be the sequence of node and labeled by the traversal method used, as shown in the sample output below.
Sample:
 Test Case #1

     [Test input]
    +---------------------------------------------------------------------
    |9 5 5 6 3 2 9 3 3 2
    +---------------------------------------------------------------------


     [stdout output]
    +---------------------------------------------------------------------
    |Inorder: 2 2 3 3 3 5 5 6 9
    |Preorder: 5 5 3 2 2 3 3 6 9
    |Postorder: 2 3 3 2 3 5 9 6 5
    |
    +---------------------------------------------------------------------
             ,        ,          ,       ,     ,          Right-heavy tree
 、My answer
#include
using namespace std;
 
struct node {
    int data;
    node* left;
    node* right;
    node(int n, node* l = NULL, node* r = NULL):data(n), left(l), right(r) {}
};
 
void Preorder(node* root);
void Inorder(node* root);
void Postorder(node* root);
void buildTree(node **head, int value);
void buildTree(node **head, int value) {
    if (*head == NULL) {
        *head = new node(value);
        (*head)->data = value;
    } else {
        if ((*head)->data < value) {
            buildTree(&((*head)->right), value);
        } else {
            buildTree(&((*head)->left), value);
        }
    }
}
void Preorder(node* root) {
    if (root != NULL) {
        cout << " " << root->data;
        Preorder(root->left);
        Preorder(root->right);
    }
}
void Inorder(node* root) {
        if (root != NULL) {
                Inorder(root->left);
        cout << " " << root->data;
        Inorder(root->right);
    }
}
void Postorder(node* root) {
        if (root != NULL) {
        Postorder(root->left);
        Postorder(root->right);
        cout << " " << root->data;
    }
}
void destory(node* &head);
void destory(node* &head) {
    if (head != NULL) {
        destory(head->left);
        destory(head->right);
        delete head;
    }
}
int main() {
    int num;
    int n;
    node* root;
    root = NULL;
    cin >> num;
    while (num--) {
        cin >> n;
        buildTree(&root, n);
    }
    cout << "Inorder:";
    Inorder(root);
    cout << endl;
    cout << "Preorder:";
    Preorder(root);
    cout << endl;
    cout << "Postorder:";
    Postorder(root);
    cout << endl;
    destory(root);
}