HashMap之TreeNode

HashMap之TreeNode


2024年2月13日发(作者:)

1 static TreeNode balanceInsertion(TreeNode root,

2 TreeNode x) {

3 = true;

4 for (TreeNode xp, xpp, xppl, xppr;;) {

5 if ((xp = ) == null) {

6 = false;

7 return x;

8 }

9 else if (! || (xpp = ) == null)

10 return root;

11 if (xp == (xppl = )) {

12 if ((xppr = ) != null && ) { //第1种情况

13 = false;

14 = false;

15 = true;

16 x = xpp;

17 }

18 else {

19 if (x == ) { //第2种情况

20 root = rotateLeft(root, x = xp);

21 xpp = (xp = ) == null ? null : ;

22 }

23 if (xp != null) { //第3种情况

24 = false;

25 if (xpp != null) {

26 = true;

27 root = rotateRight(root, xpp);

28 }

29 }

30 }

31 }

32 else {

33 if (xppl != null && ) { //第1种情况

34 = false;

35 = false;

36 = true;

37 x = xpp;

38 }

39 else {

40 if (x == ) { //第2种情况

41 root = rotateRight(root, x = xp);

42 xpp = (xp = ) == null ? null : ;

43 }

44 if (xp != null) { //第3种情况

45 = false;

46 if (xpp != null) {

47 = true;

48 root = rotateLeft(root, xpp);

49 }

50 }

51 }

52 }

53 }

54 }

1 static TreeNode balanceDeletion(TreeNode root,

2 TreeNode x) {

3 for (TreeNode xp, xpl, xpr;;) {

4 if (x == null || x == root)

5 return root;

6 else if ((xp = ) == null) {

7 = false;

8 return x;

9 }

10 else if () {

11 = false;

12 return root;

13 }

14 else if ((xpl = ) == x) {

15 if ((xpr = ) != null && ) {

16 = false;

17 = true;

18 root = rotateLeft(root, xp);

19 xpr = (xp = ) == null ? null : ;

20 }

21 if (xpr == null)

22 x = xp;

23 else {

24 TreeNode sl = , sr = ;

25 if ((sr == null || !) &&

26 (sl == null || !)) {

27 = true;

28 x = xp;

29 }

30 else {

31 if (sr == null || !) {

32 if (sl != null)

33 = false;

34 = true;

35 root = rotateRight(root, xpr);

36 xpr = (xp = ) == null ?

37 null : ;

38 }

39 if (xpr != null) {

40 = (xp == null) ? false : ;

41 if ((sr = ) != null)

42 = false;

43 }

44 if (xp != null) {

45 = false;

46 root = rotateLeft(root, xp);

47 }

48 x = root;

49 }

50 }

51 }

52 else { // 这个else的代码与if是对称的


发布者:admin,转转请注明出处:http://www.yc00.com/news/1707782188a1524013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信