Esempio n. 1
0
        private TTreeNode <T> GetTTree(BSTNode <T> BSTnode, ref TTreeNode <T> pre)
        {
            if (BSTnode == null)
            {
                return(null);
            }

            var node = new TTreeNode <T>(BSTnode.Element);

            node.Left = GetTTree(BSTnode.LeftChild, ref pre);
            if (pre != null && !pre.HasRight)
            {
                pre.Right = node;
            }
            if (node.Left != null)
            {
                node.HasLeft = true;
            }
            else
            {
                node.Left = pre;
            }
            pre        = node;
            node.Right = GetTTree(BSTnode.RightChild, ref pre);
            if (node.Right != null)
            {
                node.HasRight = true;
            }
            return(node);
        }
Esempio n. 2
0
 public static TTreeNode <T> GetPre(TTreeNode <T> node)
 {
     if (!node.HasLeft)
     {
         return(node.Left);
     }
     else
     {
         var p = node.Left;
         if (p == null)
         {
             return(p);
         }
         while (p.HasRight)
         {
             p = p.Right;
         }
         return(p);
     }
 }
Esempio n. 3
0
        private TTreeNode <T> Insert(TTreeNode <T> root, T val)
        {
            if (root == null)
            {
                return(new TTreeNode <T>(val));
            }

            if (val.CompareTo(root.Val) < 0)
            {
                if (root.HasLeft)
                {
                    root.Left = Insert(root.Left, val);
                }
                else
                {
                    var node = new TTreeNode <T>(val);
                    node.Left  = root.Left;
                    root.Left  = node;
                    node.Right = root;
                }
            }
            else if (val.CompareTo(root.Val) > 0)
            {
                if (root.HasRight)
                {
                    root.Right = Insert(root.Right, val);
                }
                else
                {
                    var node = new TTreeNode <T>(val);
                    node.Left  = root;
                    node.Right = root.Right;
                    root.Right = node;
                }
            }
            return(root);
        }
Esempio n. 4
0
        public TTree(BSTree <T> BSTree)
        {
            TTreeNode <T> pre = null;

            Root = GetTTree(BSTree.Root, ref pre);
        }
Esempio n. 5
0
 public void Insert(T val)
 {
     Root = Insert(Root, val);
 }