开发者

Binary tree, deleting item and reconnecting node

开发者 https://www.devze.com 2023-01-20 15:09 出处:网络
I\'m learning data structures and found out that for binary search trees, there are two ways to 开发者_StackOverflow社区reconnect node when you delete item. Are those two ways (below) correct?

I'm learning data structures and found out that for binary search trees, there are two ways to 开发者_StackOverflow社区reconnect node when you delete item. Are those two ways (below) correct?

Binary tree, deleting item and reconnecting node

Link to the image to see it non-resized


Yes, they are. Note that you could also do the "mirror image" version of each way, so it's actually 4 ways in total.

In fact, there are quite few ways that would produce a valid binary tree. All you need to take care of is that the left child of a node is less than the node itself, and the right child is more. However the ways you have listed are the simplest ones that are typically used (unless it's a balanced tree and you need to rebalance it).


The two methods look correct. The first method does re-balance the tree while the second simply does the connect.

0

精彩评论

暂无评论...
验证码 换一张
取 消