Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize binary Tree balance in Java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Java in how to achieve binary tree balance, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The basic idea of binary tree balance is to make the absolute value of the balance factor less than 1 by rotation.

As shown in the figure:

Input: unbalanced node z

Output: the root node of the balanced subtree

Private BinTreeNode rotate (BinTreeNode z) {BinTreeNode y = higherSubT (z); / / take y as the higher z child BinTreeNode x = higherSubT (y); / / take the x higher child boolean isLeft = z.isLChild (); / / record whether z is the left child BinTreeNode p = z.getParent (); / / the father of z is BinTreeNode a, b, c; / / from left to right, three nodes BinTreeNode t0, T1, T2, T3 / / from left to right, four subtrees / / rename if (y.isLChild ()) {/ / if y is a left child, then c = z; T3 = z.getRChild (); if (x.isLChild ()) {/ / if x is a left child (left and left imbalance) b = y; T2 = y.getRChild (); a = x; T1 = x.getRChild (); t0 = x.getLChild () } else {/ / if x is a right child (left and right imbalance) a = y; t 0 = y.getLChild (); b = x; T1 = x.getLChild (); T2 = x.getRChild ();}} else {/ / if y is a right child, a = z; t 0 = z.getLChild (); if (x.isRChild ()) {/ / if x is a right child (right imbalance) b = y; T1 = y.getLChild () C = x; T2 = x.getLChild (); T3 = x.getRChild ();} else {/ / if x is a left child (right and left imbalance) c = y; T3 = y.getRChild (); b = x; T1 = x.getLChild (); T2 = x.getRChild ();} / / remove three nodes z.sever (); y.sever (); x.sever () / / pick four subtrees: if (t0 trees null) t0.sever (); if (T1 trees null) t1.sever (); if (T2 trees null) t2.sever (); if (T2 trees null) t3.sever (); / / relink a.setLChild (T1); a.setRChild (T1); c.setLChild (T2); c.setRChild (T3); b.setLChild (a); b.setRChild (c) / / reconnect the subtree to the original tree if (paired null) if (isLeft) p.setLChild (b); else p.setRChild (b); return bespoke / return the new subtree root} / / return the subtree private BinTreeNode higherSubT (BinTreeNode v) {if (v==null) return null;int lH = (v.hasLChild ()) with higher node v? V.getLChild () .int ():-1 v.getLChild rH = (v.hasRChild ())? V.getRChild (). GetHeight ():-1 if (lH > rH) return v.getLChild (); if (lH)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report