Esempio n. 1
0
        public bool Insert(int val)
        {
            if (root == null)
            {
                root = new btreenode(val, srwlck.CreateNew());
            }
            btreenode btn = root;
            LockHelper prevLock = null;
            try
            {
                while (true)
                {
                    btn.srwlck.EnterWrite();
                    prevLock = btn.srwlck;
                    if (btn.value == val)
                    {
                        return true;
                    }
                    if (val > btn.value)
                    {
                        if (btn.right != null)
                        {
                            btn = btn.right;
                        }
                        else
                        {
                            btn.right = new btreenode(val, srwlck.CreateNew());
                            return true;
                        }
                    }
                    else if (val < btn.value)
                    {
                        if (btn.left != null)
                        {
                            btn = btn.left;
                        }
                        else
                        {
                            btn.left = new btreenode(val, srwlck.CreateNew());
                            return true;
                        }
                    }
                    prevLock.LeaveWrite();

                }
            }
            finally
            {
                prevLock.LeaveWrite();
            }
        }
Esempio n. 2
0
        private btreenode FindNodeAndParent(int val, btreenode btn, out btreenode parent)
        {
            btn.srwlck.EnterRead();
            try
            {
                parent = null;
                while (btn != null)
                {

                    if (btn.value == val)
                    {
                        return btn;
                    }
                    if (btn.left == null && btn.right == null)
                        return null;
                    if (btn.left == null && val < btn.value)
                        return null;
                    if (btn.right == null && val > btn.value)
                        return null;

                    if (btn.left != null && val < btn.value)
                    {
                        parent = btn;
                        btn = btn.left;
                        parent.srwlck.LeaveRead();
                        btn.srwlck.EnterRead();
                    }
                    else if (btn.right != null && val > btn.value)
                    {
                        parent = btn;
                        btn = btn.right;
                        parent.srwlck.LeaveRead();
                        btn.srwlck.EnterRead();
                    }
                }
            }
            finally
            {
                btn.srwlck.LeaveRead();
            }
            parent = btn;
            return null;
        }