简体   繁体   中英

Could anyone explain me my homework about binary tree? i think it weird

I have a tasks to build binary tree as follow:

  1. Build ideal balanced tree.
  2. Find amount of nodes with specified value
  3. Transform tree to binary search tree

So... What's weird for me: Everywhere i read about binary trees they never contain nodes with duplicate values, but in second task i must find amount of nodes that math to entered value... isn't it just 0 or 1 if i build tree by rules?

Or, maybe, the first tree i build must be unordered and allows to have duplicates? If i rebuild tree into binary search tree i need just delete duplicates and order nodes by left-less right-more rule?

There's no restriction on binary trees on duplicate values. A binary tree is a tree where each node has at most 2 children. That's it.

A binary search tree may have duplicates and an example for that are multiset and multimap. Simply define if you put elements with equal key in the right or in the left subtree of a given node and nothing substantial will change.

EDIT: Also in the second task it is not required that you have binary search tree. If I get it right you only have a binary tree there(ie it is not ordered)

您可以轻松地将规则之一设置为> =而不是>,从而轻松找到所有元素...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM