示例#1
0
 public bool InsertData(T x)
 {
     if (x.CompareTo(Data) == 0)
     {
         return(false);
     }
     if (x.CompareTo(Data) < 0)
     {
         if (PLeft == null)
         {
             PLeft = new MyTNode <T>(x);
         }
         else
         {
             return(PLeft.InsertData(x));
         }
     }
     else
     {
         if (pRight == null)
         {
             pRight = new MyTNode <T>(x);
         }
         else
         {
             return(pRight.InsertData(x));
         }
     }
     return(true);
 }
示例#2
0
 public int CountChildren(MyTNode <T> node)
 {
     if (node == null)
     {
         return(0);
     }
     return(((node.PLeft == null) ? 0 : CountChildren(node.PLeft) + 1) + ((node.PRight == null) ? 0 : CountChildren(node.PRight) + 1));
 }
示例#3
0
 public int ChieuCao(MyTNode <T> c)
 {
     if (c != null)
     {
         var a   = ChieuCao(PLeft);
         var b   = ChieuCao(pRight);
         var max = (a > b) ? a : b;
         return(1 + max);
         //return Math.Max(ChieuCao(pLeft), ChieuCao(pRight)) + 1;
     }
     return(0);
 }
示例#4
0
 public int Count_nut_co_1_con(MyTNode <T> node)
 {
     if (node == null)
     {
         return(0);
     }
     if ((node.PLeft != null && node.PRight == null) || (node.PLeft == null && node.PRight != null))
     {
         return(1 + Count_nut_co_1_con(node.PLeft) + Count_nut_co_1_con(node.PRight));
     }
     else
     {
         return(Count_nut_co_1_con(node.PLeft) + Count_nut_co_1_con(node.PRight));
     }
 }
示例#5
0
 public MyTNode <T> SearchLCA(MyTNode <T> searchTree, T compareValue1, T compareValue2)
 {
     if (searchTree == null)
     {
         return(null);
     }
     if (searchTree.Data.CompareTo(compareValue1) > 0 && searchTree.Data.CompareTo(compareValue2) > 0)
     {
         return(SearchLCA(searchTree.PLeft, compareValue1, compareValue2));
     }
     if (searchTree.Data.CompareTo(compareValue1) < 0 && searchTree.Data.CompareTo(compareValue2) < 0)
     {
         return(SearchLCA(searchTree.PRight, compareValue1, compareValue2));
     }
     return(searchTree);
 }
示例#6
0
 public int CountLeaf(MyTNode <T> T)
 {
     if (T == null)
     {
         return(0);
     }
     else
     if (IsLeaf(T))
     {
         return(1);
     }
     else
     {
         return(CountLeaf(T.PLeft) + CountLeaf(T.PRight));
     }
 }
示例#7
0
 public bool Insert(T x)
 {
     if (root == null)
     {
         root = new MyTNode <T>(x);
         return(true);
     }
     else
     {
         if (root.InsertData(x))
         {
             return(true);
         }
     }
     return(false);
 }
示例#8
0
 //public MyTNode<T> Max()
 //{
 //    if (root == null)
 //        return null;
 //    return root.RightMost();
 //}
 //public MyTNode<T> Min()
 //{
 //    if (root == null)
 //        return null;
 //    return root.LeftMost();
 //}
 //xóa một node trên cây
 public void Remove(ref MyTNode <T> Parent, T key)
 {
     if (Parent == null)
     {
         return;
     }
     if (key.CompareTo(Parent.Data) < 0)
     {
         Remove(ref Parent.pLeft, key);
     }
     else if (key.CompareTo(Parent.Data) > 0)
     {
         Remove(ref Parent.pRight, key);
     }
     else if (Parent.pLeft == null && Parent.pRight == null)
     {
         Parent = null;
     }
     else if (Parent.pLeft == null && Parent.pRight != null)
     {
         Parent = Parent.pRight;
     }
     else if (Parent.pLeft != null && Parent.pRight == null)
     {
         Parent = Parent.pLeft;
     }
     else
     {
         var p = Parent.pLeft;
         while (p.pRight != null)
         {
             p = p.pRight;
         }
         Parent.data = p.data;
         Remove(ref Parent.pLeft, p.data);
     }
 }
示例#9
0
 public MyTNode(T x)
 {
     Data   = x;
     PLeft  = null;
     pRight = null;
 }
示例#10
0
 //Kiem tra  nut la
 public bool IsLeaf(MyTNode <T> T)
 {
     return((T.PLeft == null) && (T.PRight == null));
 }
示例#11
0
 public void DeleteTree()
 {
     root = null;
 }
示例#12
0
            public static void Print(MyTNode <T> root, int topMargin = 3, int leftMargin = 3)
            {
                if (root == null)
                {
                    return;
                }
                var rootTop = Console.CursorTop + topMargin;
                var last    = new List <NodeInfo>();
                var next    = root;

                for (int level = 0; next != null; level++)
                {
                    var item = new NodeInfo {
                        Node = next, Text = next.Data.ToString()
                    };
                    if (level < last.Count)
                    {
                        item.StartPos = last[level].EndPos + 1;
                        last[level]   = item;
                    }
                    else
                    {
                        item.StartPos = leftMargin;
                        last.Add(item);
                    }
                    if (level > 0)
                    {
                        item.Parent = last[level - 1];
                        if (next == item.Parent.Node.PLeft)
                        {
                            item.Parent.Left = item;
                            item.EndPos      = Math.Max(item.EndPos, item.Parent.StartPos);
                        }
                        else
                        {
                            item.Parent.Right = item;
                            item.StartPos     = Math.Max(item.StartPos, item.Parent.EndPos);
                        }
                    }
                    next = next.PLeft ?? next.PRight;
                    for (; next == null; item = item.Parent)
                    {
                        Print(item, rootTop + 2 * level);
                        if (--level < 0)
                        {
                            break;
                        }
                        if (item == item.Parent.Left)
                        {
                            item.Parent.StartPos = item.EndPos;
                            next = item.Parent.Node.PRight;
                        }
                        else
                        {
                            if (item.Parent.Left == null)
                            {
                                item.Parent.EndPos = item.StartPos;
                            }
                            else
                            {
                                item.Parent.StartPos += (item.StartPos - item.Parent.EndPos) / 2;
                            }
                        }
                    }
                }
                Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
            }