Exemplo n.º 1
0
 public static bool IsBinarySearchTree(MyTreeNode <T> root)
 {
     if (root == null)
     {
         return(false);
     }
     if (root.LNode == null && root.RNode == null)
     {
         return(true);
     }
     if (root.LNode != null && root.RNode != null)
     {
         if (root.Value.CompareTo(root.LNode.Value) >= 0 && root.Value.CompareTo(root.RNode.Value) <= 0)
         {
             return(IsBinarySearchTree(root.LNode) && IsBinarySearchTree(root.RNode));
         }
         return(false);
     }
     if (root.LNode != null)
     {
         if (root.Value.CompareTo(root.LNode.Value) < 0)
         {
             return(false);
         }
         return(IsBinarySearchTree(root.LNode));
     }
     if (root.RNode != null)
     {
         if (root.Value.CompareTo(root.RNode.Value) > 0)
         {
             return(false);
         }
         return(IsBinarySearchTree(root.RNode));
     }
 }
Exemplo n.º 2
0
        public static MyTreeNode <T> CreateNormalTree(List <T> elements)
        {
            if (elements.Count == 0)
            {
                return(null);
            }
            int elementCount = elements.Count;
            int stopIndex    = (elementCount + 1) / 2;

            MyTreeNode <T>[] listNodes = new MyTreeNode <T> [elementCount];
            listNodes[0] = new MyTreeNode <T>(elements[0]);
            for (int i = 0; i < stopIndex; i++)
            {
                MyTreeNode <T> node   = listNodes[i];
                int            lIndex = 2 * i + 1;
                if (lIndex < elementCount)
                {
                    MyTreeNode <T> lNode = new MyTreeNode <T>(elements[lIndex]);
                    node.LNode        = lNode;
                    listNodes[lIndex] = lNode;
                }
                int rIndex = 2 * (i + 1);
                if (rIndex < elementCount)
                {
                    MyTreeNode <T> rNode = new MyTreeNode <T>(elements[rIndex]);
                    node.RNode        = rNode;
                    listNodes[rIndex] = rNode;
                }
            }
            return(listNodes[0]);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            List <int> elements = new List <int>()
            {
                66, 44, 22, 88, 55, 99, 77, 11, 33, 48
            };
            MyTreeNode <int> root = NodeController <int> .CrateBinarySearchTree(elements);

            List <string> strElements = new List <string>()
            {
                "Foo", "Afc", "Beyond", "Tarmar", "Coug", "Jerk", "Hour", "Ade"
            };
            MyTreeNode <string> strRoot = NodeController <string> .CrateBinarySearchTree(strElements);

            List <int> norElements = new List <int>()
            {
                66, 44, 88, 22, 55, 77, 99, 11, 33, 48, 56
            };
            MyTreeNode <int> norRoot = NodeController <int> .CreateNormalTree(norElements);

            bool ret = NodeController <int> .IsBinarySearchTree(root);

            bool norRet = NodeController <int> .IsBinarySearchTree(norRoot);

            root.LNode.RNode.Value = 45;
            ret = NodeController <int> .IsBinarySearchTree(root);
        }
Exemplo n.º 4
0
        public static MyTreeNode <T> CrateBinarySearchTree(List <T> elements)
        {
            if (elements.Count == 0)
            {
                return(null);
            }
            MyTreeNode <T> root = new MyTreeNode <T>(elements[0]);

            for (int i = 1; i < elements.Count; i++)
            {
                InsertNode(root, elements[i]);
            }
            return(root);
        }
Exemplo n.º 5
0
 private static bool InsertNode(MyTreeNode <T> curNode, T element)
 {
     if (curNode.Value.CompareTo(element) > 0)
     {
         if (curNode.LNode == null)
         {
             curNode.LNode = new MyTreeNode <T>(element);
             return(true);
         }
         return(InsertNode(curNode.LNode, element));
     }
     else
     {
         if (curNode.RNode == null)
         {
             curNode.RNode = new MyTreeNode <T>(element);
             return(true);
         }
         return(InsertNode(curNode.RNode, element));
     }
 }
Exemplo n.º 6
0
 public MyTreeNode(T element)
 {
     Value = element;
     LNode = null;
     RNode = null;
 }