2023 Huawei OD Machine Test Paper C [Calculate the height of the trigeminal search tree] Python implementation

Table of contents

topic

Ideas

Code


topic

The rules for constructing a ternary search tree are defined as follows:
    Each node stores a number. When a new number is inserted, search downwards from the root node until a suitable one is found. The empty node is inserted.
    The search rule is:
        1. If the number is less than the number of the node minus 500, insert the number into the left subtree of the node
        2. If the number is greater than the number of the node plus 500, insert the number into the right subtree of the node
        3. Otherwise, insert the number into the neutron tree of the node< a i=6> You are given a series of numbers. Please insert the numbers into the tree in order according to the above rules to construct a ternary search tree, and finally output the height of the tree.

Enter description

The first line is a number N, indicating that there are N numbers, 1<=N<=10000
The second line is N space-separated integers, each number ranges from [1,10000]

Output description

Output the height of the tree (the height of the root node is 1)

Example 1

enter

5
5000 2000 5000 8000 1800

おすすめ

転載: blog.csdn.net/misayaaaaa/article/details/135014083