Right-heavy tree——笛卡尔树

一、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<iostream>
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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值