Esempio n. 1
0
        public int[] ToArray()
        {
            int[]  newArr  = new int[Count];
            NodeBT current = Root;

            newArr[0] = Root.Data;
            int i = 1;

            do
            {
                if (current.Data != Root.Data)
                {
                    newArr[i] = current.Data;
                }
                i++;
                if (current.Left != null)
                {
                    current = current.Left;
                }
                else if (current.Right != null)
                {
                    current = current.Right;
                }
                else if (current.Left == null && current.Right == null)
                {
                    current = Root;
                }
            }while (i < Count);
            return(newArr);
        }
Esempio n. 2
0
        public void Add(int data)
        {
            if (Root == null)
            {
                NodeBT newNode = new NodeBT(data);
                Root = newNode;
                Count++;
                return;
            }

            NodeBT currentNode = Root;
            bool   addData     = false;

            do
            {
                if (data < currentNode.Data)
                {
                    if (currentNode.Left == null)
                    {
                        NodeBT newNode = new NodeBT(data);
                        currentNode.Left = newNode;
                        addData          = true;
                        Count++;
                    }
                    else
                    {
                        currentNode = currentNode.Left;
                    }
                }
                if (data > currentNode.Data)
                {
                    if (currentNode.Right == null)
                    {
                        NodeBT newNode = new NodeBT(data);
                        currentNode.Right = newNode;
                        addData           = true;
                        Count++;
                    }
                    else
                    {
                        currentNode = currentNode.Right;
                    }
                }
            }while (!addData);
        }
Esempio n. 3
0
        public bool Contains(int data)
        {
            NodeBT currentNode  = Root;
            bool   containsData = false;

            do
            {
                if (currentNode.Data == data)
                {
                    containsData = true;
                    return(containsData);
                }
                if (data < currentNode.Data)
                {
                    currentNode = currentNode.Left;
                }
                if (data > currentNode.Data)
                {
                    currentNode = currentNode.Right;
                }
            }while (!containsData);
            return(containsData);
        }
Esempio n. 4
0
 public BinaryTree(int data)
 {
     Root = new NodeBT(data);
 }
Esempio n. 5
0
 public NodeBT(int data)
 {
     Data  = data;
     Left  = null;
     Right = null;
 }