Exemplo n.º 1
0
        public bool SearchNodeHere(NodeTree <T> searched)
        {
            if (this.CompareTo(searched) == 0)
            {
                return(true);
            }
            else if (this.CompareTo(searched) < 0)
            {
                if (this.Right == null)
                {
                    return(false);
                }

                this.Right.SearchNodeHere(searched);
            }
            else
            {
                if (this.Left == null)
                {
                    return(false);
                }

                this.Left.SearchNodeHere(searched);
            }

            return(false);
        }
Exemplo n.º 2
0
        public void AddNodeHere(NodeTree <T> forAdd)
        {
            if (this.CompareTo(forAdd) == 0)
            {
                throw new ArgumentException("Nodes are equals!");
            }

            if (this.CompareTo(forAdd) < 0)
            {
                if (this.Right == null)
                {
                    this.Right = forAdd;
                }
                else
                {
                    this.Right.AddNodeHere(forAdd);
                }
            }
            else
            {
                if (this.Left == null)
                {
                    this.Left = forAdd;
                }
                else
                {
                    this.Left.AddNodeHere(forAdd);
                }
            }
        }
Exemplo n.º 3
0
        public void FindAndRemove(NodeTree <T> forRemove)
        {
            if (this.CompareTo(forRemove) < 0)
            {
                if (this.Right == null)
                {
                    return;
                }

                if (this.Right.CompareTo(forRemove) == 0)
                {
                    this.Right = this.Right.RemoveThisNode();
                }
                else
                {
                    this.Right.FindAndRemove(forRemove);
                }
            }
            else
            {
                if (this.Left == null)
                {
                    return;
                }

                if (this.Left.CompareTo(forRemove) == 0)
                {
                    this.Left = this.Left.RemoveThisNode();
                }
                else
                {
                    this.Left.FindAndRemove(forRemove);
                }
            }
        }
Exemplo n.º 4
0
 // need reworking still but working
 public NodeTree <T> RemoveThisNode()
 {
     if (this.Left == null & this.Right == null)
     {
         return(null);
     }
     else if (this.Left == null)
     {
         return(this.Right);
     }
     else if (this.Right == null)
     {
         return(this.Left);
     }
     else
     {
         T minData = this.Right.FindMin();
         this.Data = minData;
         if (this.Right.Data.Equals(minData))
         {
             this.Right = this.Right.RemoveThisNode();
         }
         else
         {
             this.Right.RemoveMin(minData);
         }
         return(this);
     }
 }
Exemplo n.º 5
0
        public bool Search(T data)
        {
            var searched = new NodeTree <T>(data);

            if (this.IsEmpty())
            {
                return(false);
            }
            else
            {
                return(this.root.SearchNodeHere(searched));
            }
        }
Exemplo n.º 6
0
        public void Add(T data)
        {
            var forAdd = new NodeTree <T>(data);

            if (this.IsEmpty())
            {
                this.root = forAdd;
            }
            else
            {
                root.AddNodeHere(forAdd);
            }
        }
Exemplo n.º 7
0
        public void Remove(T data)
        {
            var forRemove = new NodeTree <T>(data);

            if (this.IsEmpty())
            {
                return;
            }
            else
            {
                if (this.root.Equals(forRemove))
                {
                    this.Root = this.Root.RemoveThisNode();
                }
                else
                {
                    this.Root.FindAndRemove(forRemove);
                }
            }
        }
Exemplo n.º 8
0
 private BinaryTree(NodeTree <T> root)
 {
     this.root = new NodeTree <T>(root);
 }
Exemplo n.º 9
0
 public int CompareTo(NodeTree <T> other)
 {
     return(this.Data.CompareTo(other.Data));
 }
Exemplo n.º 10
0
 internal NodeTree(NodeTree <T> forCopy)
 {
     this.Data  = forCopy.Data;
     this.Left  = forCopy.Left?.DeepCopy();
     this.Right = forCopy.Right?.DeepCopy();
 }