using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System.Diagnostics; using System.Net; using System.Text; using System.Xml; public class MyNode { public MyNode(string s) { Value = int.Parse(s); } public MyNode(int s) { Value = s; } public MyNode Left; public MyNode Right; public int Value; public int 左右节点层级差值 { get { if (this == null) { return int.MaxValue; } else { int l = Left != null ? Left.最大层级 : 0; int r = Right != null ? Right.最大层级 : 0; return l - r; } } } /// <summary> /// 当前有多少子层,孙层 /// </summary> public int 最大层级 { get { if (this == null) { return 0; } else { int max = 1; if (Left != null && Right != null) { int l = Left.最大层级; int r = Right.最大层级; max += Math.Max(l, r); } else { if(Left!=null) { max+=Left.最大层级; } else if(Right!=null) { max+=Right.最大层级; } } return max; } } } #region 暂时无用的东西 public MyNode 最小的孙子 { get { var t = this; while (t.Left != null) { t = t.Left; } return t; } } public MyNode 最大的孙子 { get { var t = this; while (t.Right != null) { t = t.Right; } return t; } } public int 直接子节点数量 { get { int i = 0; if (Left != null) { i++; } if (Right != null) { i++; } return i; } } public MyNode Next { get { if (Left != null && Right == null) { return Left; } if (Left == null && Right != null) { return Right; } return null; } } #endregion /// <summary> /// 存放父node /// </summary> public MyNode Pnode; } class MyTree { public MyNode myNode; public MyTree(string v2) { var temp = v2.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries); if (temp.Length > 0) { for (int i = 0; i < temp.Length; i++) { int v = int.Parse(temp[i]); 平衡插入(v); } } } /// <summary> /// 插入节点并返回这个节点 /// </summary> /// <param name="v"></param> /// <returns></returns> MyNode Insert(int v) { if (myNode == null) { myNode = new MyNode(v); return myNode; } MyNode temp; temp = myNode; while (temp != null) { if (temp.Value > v) { var t2 = temp.Left; if (t2 != null) { t2.Pnode = temp; temp = t2; } else { temp.Left = new MyNode(v); temp.Left.Pnode = temp; return temp.Left; } } else { var t2 = temp.Right; if (t2 != null) { t2.Pnode = temp; temp = t2; } else { temp.Right = new MyNode(v); temp.Right.Pnode = temp; return temp.Right; } } } return null; } void 平衡插入(int v) { var 麻烦节点 = Insert(v); if (麻烦节点.Pnode == null)//p为空,说明只有一个节点 { //无需操作,平衡着呢 return; } else { MyNode 发现者 = 获取不平衡节点(麻烦节点.Pnode); if (发现者 == null) { //平衡,结束操作 } else { //此时,只需要修改 发现者 以内的节点,即可做到平衡 //先把这个 麻烦节点,从树中移除掉 麻烦节点.Pnode.Left = 麻烦节点.Pnode.Right = null; if (v > 发现者.Value)//说明需要插入到根节点的右侧 { var p = 麻烦节点.Pnode; int newV = v; while (p != 发现者) { if (newV > p.Value) { int tempV = p.Value; p.Value = newV; newV = tempV; break; } else { p = p.Pnode; } } if (p == 发现者) { if (newV > p.Value) { int tempV = p.Value; p.Value = newV; newV = tempV; } } 平衡插入(newV); } if (v < 发现者.Value)//说明需要插入到根节点的右侧 { var p = 麻烦节点.Pnode; int newV = v; while (p != 发现者) { if (newV < p.Value) { int tempV = p.Value; p.Value = newV; newV = tempV; break; } else { p = p.Pnode; } } if (p == 发现者) { if (newV < p.Value) { int tempV = p.Value; p.Value = newV; newV = tempV; } } 平衡插入(newV); } } } } private MyNode 获取不平衡节点(MyNode p) { var 发现者 = p; while (发现者 != null) { if (Math.Abs(发现者.左右节点层级差值) > 1) { return 发现者; } else { 发现者 = 发现者.Pnode;//一直找父节点,知道根,根没有父节点,退出循环 } } return null; } } class T { static void Main(string[] args) { Console.ReadLine(); var v = Console.ReadLine(); MyTree tree = new MyTree(v); Console.WriteLine(tree.myNode.Value); return; } }
临时存储用,随后修改.
猜你喜欢
转载自www.cnblogs.com/interim/p/9728923.html
今日推荐
周排行