Exemple #1
0
        public LJTreeNode FindNode(ITreeNode item, LJTreeNode parent)
        {
            LJTreeNode t = parent;

            if (parent == null)
            {
                t = root;
            }
            while (t != null)
            {
                int comp = item.CompareTo(t.data);
                if (comp == 0)
                {
                    break;
                }
                else
                {
                    if (item.IsChildOf(t.data))
                    {
                        t = t.Left;
                    }
                    else if (comp > 0)
                    {
                        t = t.Right;
                    }
                    else
                    {
                        t = null;
                    }
                }
            }
            return(t);
        }
Exemple #2
0
        public LJTreeNode Insert(ITreeNode item)
        {
            LJTreeNode t       = root;
            LJTreeNode parent  = null;
            LJTreeNode right   = null;
            LJTreeNode newNode = null;

            //用<表示兄,用>表示弟,用^表示孩子
            // terminate on on empty subtree
            while (t != null)
            {
                parent = t;
                if (item.IsChildOf(t.data))
                {
                    parent = t;
                    right  = null;
                    t      = t.Left;
                }
                else if (item.CompareTo(t.data) < 0)
                {
                    parent = t.Parent;
                    right  = t;
                    break;
                }
                else
                {
                    parent = t;
                    right  = null;
                    t      = t.Right;
                }
            }
            newNode = GetTreeNode(item, parent, null, right);
            if (parent == null)
            {
                root = newNode;
            }
            else if (item.IsChildOf(parent.data))
            {
                parent.Left = newNode;
            }
            else
            {
                parent.Right = newNode;
            }
            if (right != null)
            {
                right.Parent = newNode;
            }
            current = newNode;
            size++;
            return(newNode);
        }