二叉搜索树可以既是满的又是完全的吗?

发布于 2024-09-27 23:51:29 字数 411 浏览 1 评论 0原文

在准备数据结构期中考试时,教授给我们做了去年的测试,其中一个问题涉及将示例树重新排列成完整的二叉搜索树。我尝试了几种不同版本的写出树,但是 Wolfram Mathematica 的这个完整二叉树示例根本没有帮助,因为它也符合完整的定义。教科书将完整的二叉树定义为 n-1 级的树是完美的,在 n 级有一些额外的叶节点,全部左对齐。

节点为 AEILNOPRST U,n=11 个节点。这是我想出的最佳答案:

           R
         /    \
        L      T
       / \    / \
     I    N   S   U
    / \  / \
   A  E O   P

但这适合 WM 树的示例,但不适合书本示例。那么哪个是正确答案呢?

In preparation for the data structures midterm, the professor gave us last year's test, one question which deals with re-arranging an example tree into a complete binary search tree. I've tried several different versions of writing out the tree, but this complete binary tree example from Wolfram Mathematica didn't help at all, since it also fits the definition of full. The textbook defines a complete binary tree as a tree through level n-1 is perfect with some extra leaf nodes at level n, all left aligned.

The nodes are A E I L N O P R S T U, n=11 nodes. Here's the best answer I came up with:

           R
         /    \
        L      T
       / \    / \
     I    N   S   U
    / \  / \
   A  E O   P

But this fits the example of the tree at WM, but not the book example. So which is the correct answer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

闻呓 2024-10-04 23:51:29

我不完全理解你的困惑在哪里,但我会尽力回答......

如果每个节点都有 0 或 2 个子节点,则二叉树被认为是完整的。

如果除最后一层之外的每一层都已满,并且所有节点都被推到尽可能远的左侧,则认为二叉树是完整的。

因此,如果它符合这两种描述(这是可能的),它就可以同时是完整的。

此外,如果二叉树已满并且所有叶子都位于同一级别,则该二叉树被认为是完美的。

因此,在您上面绘制的示例中,该树是完整的,但并不完美。

我希望这有帮助。

I don't completely understand where your confusion lies but I'll do my best to answer...

A binary tree is considered full if every node has exactly 0 or 2 children.

A binary tree is considered complete if every level is full except the last, and all nodes are pushed as far left as possible.

So if it fits both of these descriptions, which is possible, it can simultaneously be full and complete.

Also, a binary tree is considered perfect if it is full and all leaves are on the same level.

So in the example you drew out above, that tree is full and complete, but not perfect.

I hope this helps.

记忆里有你的影子 2024-10-04 23:51:29

希望有帮助的更多示例:

完整,不完整:

        R
      /    \
     L      T
    / \    / \
  I    N   S   U
 / \  /
A  E O   

完整,不完整:

        R
      /    \
     L      T
    / \    / \
  I    N   S   U
      / \
     O   P


        R
      /    \
     L      T
    / \    
  I    N   
 / \  / \
A  E O   P

Some more examples which will hopefully be helpful:

Complete, not full:

        R
      /    \
     L      T
    / \    / \
  I    N   S   U
 / \  /
A  E O   

Full, not complete:

        R
      /    \
     L      T
    / \    / \
  I    N   S   U
      / \
     O   P


        R
      /    \
     L      T
    / \    
  I    N   
 / \  / \
A  E O   P
嗼ふ静 2024-10-04 23:51:29

完整的树:
如果每个节点要么是叶子要么是二叉树 T 是满的
恰好拥有两个子节点。

      O
     / \
    O   O
   / \ / \
  O  O O  O
    / \
   O   O

完整树但不完整

完整树:
一个有 n 层的二叉树 T 是完整的,如果所有
除最后一个级别外,其他级别可能已完全满,
最后一层的所有节点都位于左侧。

       O
      / \
     O   O
    /
   O

完整的树但不完整

同样,另一个例子

      O
     / \
    O   O
   / \ / \
  O  O O  O
 /\ /
O O O

我希望这些有帮助!

Full Tree:
a binary tree T is full if each node is either a leaf or
possesses exactly two child nodes.

      O
     / \
    O   O
   / \ / \
  O  O O  O
    / \
   O   O

Full tree but not complete

Complete Tree:
a binary tree T with n levels is complete if all
levels except possibly the last are completely full,
and the last level has all its nodes to the left side.

       O
      / \
     O   O
    /
   O

Complete tree but not full

Similarly, another example

      O
     / \
    O   O
   / \ / \
  O  O O  O
 /\ /
O O O

I hope these are helpful !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文