| |

VerySource

 Forgot password?
 Register
Search
View: 2681|Reply: 14

Ask: How to find the rightmost node of the last layer of a complete binary tree

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-6 12:00:01
| Show all posts |Read mode
As the title.
It is easy to implement a complete binary tree based on array, just return the last item of the array.
But what algorithm should be used for a full binary tree based on pointer implementation?
Thank you very much!
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-6 17:30:01
| Show all posts
Right subtree of right subtree

Until the last layer

Isn't it OK?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-6 17:48:01
| Show all posts
No, a complete binary tree is not necessarily a full binary tree.
In the last layer, all nodes are arranged seamlessly from left to right.
Reply

Use magic Report

3

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-1-7 10:09:01
| Show all posts
Indeed, finding it with GoFastRight (); is a bit cumbersome!
Reply

Use magic Report

0

Threads

17

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-1-7 10:39:01
| Show all posts
Breadth-first search.
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Australia

Post time: 2020-1-7 12:18:01
| Show all posts
:) Use a linked list to do a hierarchical search.
On the first level, throw the root into the linked list.
In the second layer, find the L and R children of the root and replace the root in the linked list.
In the third layer, find the child nodes of the root L and R child nodes and replace them in the linked list.
In this recursion, if a node has no children, it is deleted, otherwise it is replaced by its own children. Keep child nodes in order from left to right.
This way to the last level, the last child node is what you are looking for.

Of course, when it is implemented, it is necessary to traverse one layer before deleting the previous one. :)
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-7 16:09:01
| Show all posts
Oh I see.
That is to say, the queue-based breadth-first traversal is used. Each time a visited node is popped from the queue into the stack, the data item on the top of the stack after the traversal is requested.
The complexity of this algorithm is indeed much greater than that of array-based ones.
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-8 08:45:01
| Show all posts
Last node traversed by layer
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-15 16:27:02
| Show all posts
Keep looking for the right sub-tree from the beginning. If there is no right sub-tree, look for the left sub-tree. All the way to the leaf node.
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-15 17:27:01
| Show all posts
The abovech1074856method is wrong, please ignore it.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list