Exemplo n.º 1
0
        // insert a value into the tree
        public void insert(T i)
        {
            // new node + data
            CNode <T> newNode = new CNode <T>();

            newNode.data = i;
            try
            {
                // if there is not a root node yet, root node = new node
                if (root == null)
                {
                    root = newNode;
                }
                else
                {
                    CNode <T> current = root;
                    CNode <T> parrent;
                    // check if new node should be left or right of parrent node, if new node value is lower then parrent, new node left
                    // else new node right
                    while (true)
                    {
                        parrent = current;

                        if (i.ToString().CompareTo(current.data.ToString()) > 0)
                        {
                            current = current.Left;

                            if (current == null)
                            {
                                parrent.Left = newNode;

                                break;
                            }
                        }
                        else
                        {
                            current = current.Right;

                            if (current == null)
                            {
                                parrent.Right = newNode;

                                break;
                            }
                        }
                    }
                }
            }catch (Exception e) { }
        }
Exemplo n.º 2
0
        // finds node with provided T data
        public CNode <T> Find(T data)
        {
            CNode <T> current = root;

            // check if current node data equals data
            while (!current.data.Equals(data))
            {
                // if data < node data- search left node else search rigth node
                if (data.ToString().CompareTo(current.data.ToString()) > 0)
                {
                    current = current.Left;
                }
                else
                {
                    current = current.Right;
                }
                if (current == null)
                {
                    return(null);
                }
            }
            return(current);
        }
Exemplo n.º 3
0
 // new binarysearchtree
 public binarySearchTree()
 {
     root = null;
 }