Beispiel #1
0
        private static void AddNode(BinareTreeNode <T> node, double item)
        {
            if (item.CompareTo(node.Value) == -1)
            {
                if (node.Left == null)
                {
                    node.Left = new BinareTreeNode <T>(item)
                    {
                        ParentNode = node
                    };
                }
                else
                {
                    AddNode(node.Left, item);
                }
            }

            if (item.CompareTo(node.Value) != 1)
            {
                return;
            }
            if (node.Right == null)
            {
                node.Right = new BinareTreeNode <T>(item)
                {
                    ParentNode = node
                };
            }
            else
            {
                AddNode(node.Right, item);
            }
        }
Beispiel #2
0
        private double GetMax(BinareTreeNode <T> node)
        {
            var current = node;

            while (current.Right != null)
            {
                current = current.Right;
            }
            return(current.Value);
        }
Beispiel #3
0
        public void Insert(double item)
        {
            if (Count == 0)
            {
                Head = new BinareTreeNode <T>(item);
            }
            else
            {
                AddNode(Head, item);
            }

            Count++;
        }
Beispiel #4
0
        private static void GetRoundTree(BinareTreeNode <T> node, IList <double> list)
        {
            var current = node;

            list.Add(current.Value);
            if (current.Left != null)
            {
                GetRoundTree(current.Left, list);
            }
            if (current.Right != null)
            {
                GetRoundTree(current.Right, list);
            }
        }
Beispiel #5
0
        public void GetBalance()
        {
            var result = new List <double>();

            GetRoundTree(Head, result);
            var min    = GetMin(Head);
            var max    = GetMax(Head);
            var middle = min + (max - min) / 2;

            Head  = new BinareTreeNode <T>(middle);
            Count = 1;
            foreach (var res in result)
            {
                Insert(res);
            }
        }
Beispiel #6
0
        private void ClearOneByOne(BinareTreeNode <T> node)
        {
            var current = node;

            if (current.Left != null)
            {
                ClearOneByOne(current.Left);
            }
            if (current.Right != null)
            {
                ClearOneByOne(current.Left);
            }
            node.Left       = null;
            node.Right      = null;
            node.ParentNode = null;
            node.Value      = default;
        }
Beispiel #7
0
 public void Clear()
 {
     ClearOneByOne(Head);
     Head  = null;
     Count = 0;
 }