【问题标题】:Height of Skewed Binary Search Tree倾斜二叉搜索树的高度
【发布时间】:2021-12-30 21:26:14
【问题描述】:

我已经实现了如下代码,在这里我做了两个函数来计算使用递归和不使用递归的二叉搜索树的高度。

#include <iostream>
#include <list>
using namespace std;

struct node
{
    int key;
    struct node *left, *right;
};

struct node *newNode(int item)
{
    struct node *temp = new node;
    temp->key = item;
    temp->left = temp->right = NULL;
    return temp;
}

void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf("%d ", root->key);
        inorder(root->right);
    }
}

struct node *insert(struct node *node, int key)
{
    if (node == NULL)
        return newNode(key);

    if (key < node->key)
        node->left = insert(node->left, key);
    else if (key > node->key)
        node->right = insert(node->right, key);

    return node;
}

int heightRecursive(struct node *node)
{
    if (node == NULL)
        return -1;
    else
    {
        int lDepth = heightRecursive(node->left);
        int rDepth = heightRecursive(node->right);

        if (lDepth > rDepth)
            return (lDepth + 1);
        else
            return (rDepth + 1);
    }
}

int heightNonRecursive(node* root)
{
    if (root == NULL) {
        return 0;
    }
 
    list<node*> queue;
    queue.push_back(root);
 
    node* front = NULL;
    int height = 0;
 
    while (!queue.empty())
    {
        int size = queue.size();
 
        while (size--)
        {
            front = queue.front();
            queue.pop_front();
 
            if (front->left) {
                queue.push_back(front->left);
            }
 
            if (front->right) {
                queue.push_back(front->right);
            }
        }
 
        height++;
    }
 
    return height;
}
 

int main()
{
    struct node *root = NULL;
    root = insert(root, 10);
    insert(root, 20);
    insert(root, 30);
    insert(root, 40);
    insert(root, 50);
    insert(root, 60);
    insert(root, 70);
    insert(root, 75);
    insert(root, 80);

    inorder(root);
    int h = heightRecursive(root);
    cout << "\n\nHeight of tree using recursive function: " << heightRecursive(root);
    cout << "\nHeight of tree using non-recursive function: " << heightNonRecursive(root);

    return 0;
}

我已经实现了一个像 10->20->30->40->50->60->70->75->80 这样的倾斜二叉树,但是在 heightNonRecursive() 函数中,我得到了高度这个二叉搜索树为 9。请帮助我在哪里做错了。

以上代码的输出:

10 20 30 40 50 60 70 75 80 

Height of tree using recursive function: 8
Height of tree using non-recursive function: 9

【问题讨论】:

标签: c++ data-structures tree binary-search-tree


【解决方案1】:

在不平衡树中,您有 9 个按递增顺序排列的不同数字,因此高度应为 8,这对于递归函数是正确的。

10 
 20 
  30 
   40 
    50 
     60 
      70 
       75 
        80

使用非递归函数,您只需从height = -1; 开始,如果树中只有一项,它应该返回0

int heightNonRecursive(node* root)
{
    if (root == NULL) 
        return 0;

    list<node*> queue;
    queue.push_back(root);
    node* front = NULL;
    int height = -1; //<-start at -1
    while (!queue.empty())
    {
        int size = queue.size();
        while (size--)
        {
            front = queue.front();
            queue.pop_front();
            if (front->left) 
                queue.push_back(front->left);
            if (front->right) 
                queue.push_back(front->right);
        }
        height++;
    }
    return height;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多