Пример #1
0
        public void TestIndividualOperations()
        {
            var left  = new NumberNode(27);
            var right = new NumberNode(14);

            var node     = new BinNode(left, TokenType.PLUS, right);
            var actual   = new Interpreter().visit(node);
            var expected = new Number(41);

            expected.ShouldDeepEqual(actual);

            node     = new BinNode(left, TokenType.MINUS, right);
            actual   = new Interpreter().visit(node);
            expected = new Number(13);

            expected.ShouldDeepEqual(actual);

            node     = new BinNode(left, TokenType.MULTIPLY, right);
            actual   = new Interpreter().visit(node);
            expected = new Number(378);

            expected.ShouldDeepEqual(actual);

            node     = new BinNode(left, TokenType.DIVIDE, right);
            actual   = new Interpreter().visit(node);
            expected = new Number(1.9285714285714286);

            expected.ShouldDeepEqual(actual);
        }
Пример #2
0
 //找到高度更高的子树
 private static BinNode <T> TallerChild(BinNode <T> x)
 {
     return(x.LChild.Stature() > x.RChild.Stature()
         ? x.LChild
         : (x.RChild.Stature() > x.LChild.Stature() ?
            x.RChild : x.IsLChild?x.LChild : x.RChild));
 }
Пример #3
0
        public static void BuildTree(BinNode <double> tree) // Enter -1 To End
        {
            Console.WriteLine("Current Root Is: " + tree.GetValue());
            Console.Write("Enter Left Num: ");
            double leftValue = double.Parse(Console.ReadLine());

            if (leftValue == -1)
            {
                return;
            }
            BinNode <double> left = new BinNode <double>(leftValue);

            Console.Write("Enter Right Num: ");
            double rightValue = double.Parse(Console.ReadLine());

            if (rightValue == -1)
            {
                return;
            }
            BinNode <double> right = new BinNode <double>(rightValue);

            tree.SetRight(right);
            tree.SetLeft(left);
            BuildTree(left);
            BuildTree(right);
        }
Пример #4
0
        public void TestFullExpression()
        {
            var node = new BinNode(
                new NumberNode(27),
                TokenType.PLUS,
                new BinNode(
                    new BinNode(
                        new BinNode(
                            new NumberNode(43),
                            TokenType.DIVIDE,
                            new NumberNode(36)
                            ),
                        TokenType.MINUS,
                        new NumberNode(48)
                        ),
                    TokenType.MULTIPLY,
                    new NumberNode(51)
                    )
                );

            var actual   = new Interpreter().visit(node);
            var expected = new Number(-2360.0833333333335);

            expected.ShouldDeepEqual(actual);
        }
Пример #5
0
 public BinNode(PackingObject obj, BinNode parent, Rect rect)
 {
     this.nodes  = new BinNode[2];
     this.parent = parent;
     this.obj    = obj;
     this.rect   = rect;
 }
Пример #6
0
        private static BinNode <TKey, TValue> Remove(BinNode <TKey, TValue> rootNode, TKey key)
        {
            if (rootNode == null)
            {
                return(null);
            }
            if (rootNode.Key.CompareTo(key) > 0)
            {
                rootNode.LeftNode = Remove((BinNode <TKey, TValue>)rootNode.LeftNode, key);
            }
            else if (rootNode.Key.CompareTo(key) < 0)
            {
                rootNode.RightNode = Remove((BinNode <TKey, TValue>)rootNode.RightNode, key);
            }
            else
            {
                if (rootNode.LeftNode == null)
                {
                    return((BinNode <TKey, TValue>)rootNode.LeftNode);
                }
                if (rootNode.RightNode == null)
                {
                    return((BinNode <TKey, TValue>)rootNode.RightNode);
                }
                var temp = GetMin((BinNode <TKey, TValue>)rootNode.RightNode);
                rootNode.Element   = temp.Element;
                rootNode.Key       = temp.Key;
                rootNode.RightNode = DeleteMin(rootNode.RightNode);
            }

            return(rootNode);
        }
Пример #7
0
        public Number visit(BinNode node)
        {
            Contract.Requires(node != null);

            var    left  = node.nodeA.accept(this).value;
            var    right = node.nodeB.accept(this).value;
            double value;

            if (node.operative == TokenType.PLUS)
            {
                value = left + right;
            }
            else if (node.operative == TokenType.MINUS)
            {
                value = left - right;
            }
            else if (node.operative == TokenType.MULTIPLY)
            {
                value = left * right;
            }
            else if (node.operative == TokenType.DIVIDE)
            {
                value = left / right;
            }
            else
            {
                throw new Exception($"Invalid operator {node.operative}");
            }

            return(new Number(value));
        }
Пример #8
0
        /// <summary>
        /// 查找函数
        /// <remarks>
        /// 查找到元素,如果命中,将其伸展到根结点,
        /// 否则将Hot结点伸展到跟结点
        /// </remarks>
        /// </summary>
        /// <param name="e">要查找的元素</param>
        /// <returns></returns>
        public override BinNode <T> Search(T e)
        {
            BinNode <T> p = SearchIn(Root, e);

            Root = Splay(p ?? Hot);
            return(Root);
        }
Пример #9
0
        /// <summary>
        /// 重写插入
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public override BinNode <T> Insert(T e)
        {
            if (Root == null)
            {
                Size++;
                return(Root = new BinNode <T>(e));
            }
            if (Eq(e, Search(e).Data))
            {
                return(Root);
            }
            Size++;
            BinNode <T> t = Root;

            if (Lt(Root.Data, e))
            {
                t.Parent = Root = new BinNode <T>(e, null, t, t.RChild);
                if (t.HasRChild)
                {
                    t.RChild.Parent = Root;
                    t.RChild        = null;
                }
            }
            else
            {
                t.Parent = Root = new BinNode <T>(e, null, t.LChild, t);
                if (t.HasLChild)
                {
                    t.LChild.Parent = Root;
                    t.LChild        = null;
                }
            }
            UpdateHeightAbove(t);
            return(Root);
        }
Пример #10
0
        /// <summary>
        /// 替换节点:将 beReplace 节点替换为 node
        /// </summary>
        /// <param name="node"></param>
        /// <param name="beReplace">被替换的节点</param>
        private BinNode <T> Replace(BinNode <T> node, BinNode <T> beReplace)
        {
            if (beReplace.IsRoot())
            {
                Root = node;
                if (null != Root)
                {
                    Root.ParentNode = null;
                }
                return(Root);
            }

            if (null != node)
            {
                node.ParentNode = beReplace.ParentNode;
            }
            if (beReplace.IsRChild())
            {
                beReplace.ParentNode.RightChild = node;
            }
            else
            {
                beReplace.ParentNode.LeftChild = node;
            }
            return(node);
        }
Пример #11
0
        public static int Calc(BinNode <int> exp)
        {
            // 1+, 2-, 3*, 4/
            if (IsLeaf(exp))
            {
                return(exp.GetValue());
            }
            // כאן זה לא עלה אלא פעולה ולכן יש בודאות שני הבנים
            int left  = Calc(exp.GetLeft());
            int right = Calc(exp.GetRight());
            int oper  = exp.GetValue();

            if (oper == 1)
            {
                return(left + right);
            }
            if (oper == 2)
            {
                return(left - right);
            }
            if (oper == 3)
            {
                return(left * right);
            }
            // if (oper == 4) - זה מה שנשאר
            return(left / right);
        }
Пример #12
0
 public BinNode(byte[] element, uint frequency, BinNode left = null, BinNode right = null)
 {
     Element = element;
     Freq    = frequency;
     Left    = left;
     Right   = right;
 }
Пример #13
0
        static void Main(string[] args)
        {
            //BinNode<int> root = new BinNode<int>(1);
            //BinNode<int> left = new BinNode<int>(2);
            //BinNode<int> right = new BinNode<int>(3);

            //root.SetLeft(left);
            //root.SetRight(right);
            //Console.WriteLine(root);

            //        BinNode<string> root = CreateStringTree();
            BinNode <int> tree = RandomCreateTree(2, new Random(), 10, 80);

            Console.WriteLine(tree);
            //PrintEvenNodes(tree);
            BinNode <int> r   = null;
            Random        rnd = new Random();

            for (int i = 0; i < 10; i++)
            {
                r = CreateSearchTree(r, rnd.Next(1, 101));
            }

            BinNode <int> t    = new BinNode <int>(54);
            BinNode <int> left = new BinNode <int>(null, 48, new BinNode <int>(54));

            t.SetLeft(left);
            BinNode <int> right = new BinNode <int>(new BinNode <int>(63), 77, null);

            t.SetRight(right);

            Console.WriteLine(r);
            PrintInOrderTree(r);
        }
Пример #14
0
        private static void Main()
        {
            var dNode = new BinNode <char, int>('D', 14);
            var bNode = new BinNode <char, int>('B', 13, null, dNode);
            var hNode = new BinNode <char, int>('H', 22);
            var iNode = new BinNode <char, int>('I', 41);
            var fNode = new BinNode <char, int>('F', 44, hNode, iNode);
            var gNode = new BinNode <char, int>('G', 22);
            var eNode = new BinNode <char, int>('E', 54, gNode, null);
            var cNode = new BinNode <char, int>('C', 12, eNode, fNode);
            var aNode = new BinNode <char, int>('A', 23, bNode, cNode);


            BinNode <char, int> .Preorder(aNode);

            Console.ReadLine();
            Console.Clear();
            BinNode <char, int> .Inorder(aNode);

            Console.ReadLine();
            Console.Clear();
            BinNode <char, int> .Postorder(aNode);

            Console.ReadLine();
            Console.Clear();
            Console.WriteLine(BinNode <char, int> .Search(aNode, 'F'));
            Console.ReadLine();
            Console.Clear();
        }
Пример #15
0
        public static void PrintTreeWithLevels <T>(BinNode <T> t)
        {
            //תור עזר של צמתים
            Queue <BinNode <T> > qNodes = new Queue <BinNode <T> >();
            Queue <int>          levels = new Queue <int>();

            //הכנסת השורש
            qNodes.Insert(t);
            int currLevel = 0;

            levels.Insert(currLevel);
            while (!qNodes.IsEmpty())
            {
                //נשלוף את הצומת
                BinNode <T> temp = qNodes.Remove();
                currLevel = levels.Remove();
                //קוד לביצוע
                Console.WriteLine(temp.GetValue() + "-" + "level:" + currLevel);
                if (temp.HasLeft())
                {
                    qNodes.Insert(t.GetLeft());
                    levels.Insert(currLevel + 1);
                }
                if (temp.HasRight())
                {
                    qNodes.Insert(t.GetRight());
                    levels.Insert(currLevel + 1);
                }
            }
        }
Пример #16
0
 public static bool IsBst(BinNode <int> t)
 {
     //אם עץ ריק
     if (t == null)
     {
         return(true);
     }
     //אם יש ילד שמאלי גדול מהשורש הנוכחי
     if (t.HasLeft())
     {
         if (t.GetValue() <= t.GetLeft().GetValue())
         {
             return(false);
         }
     }
     //אם יש ילד ימני קטן מהשורש הנוכחי
     if (t.HasRight())
     {
         if (t.GetValue() > t.GetRight().GetValue())
         {
             return(false);
         }
     }
     //נבדק שגם תת עץ שמאל וגם תת עץ ימין הם עץ חיפוש
     return(IsBst(t.GetLeft()) && IsBst(t.GetRight()));
 }
Пример #17
0
 /// <summary>
 /// 按照3+4规则旋转 按照中序遍历的规则使之能够达到
 /// t0->a->t1->b->t2->c->t3
 /// </summary>
 /// <param name="a">a</param>
 /// <param name="b">b</param>
 /// <param name="c">c</param>
 /// <param name="t0">t0</param>
 /// <param name="t1">t1</param>
 /// <param name="t2">t2</param>
 /// <param name="t3">t3</param>
 /// <returns>返回局部子树的根节点</returns>
 private BinNode <T> Connect34(BinNode <T> a, BinNode <T> b, BinNode <T> c,
                               BinNode <T> t0, BinNode <T> t1, BinNode <T> t2, BinNode <T> t3)
 {
     a.LChild = t0;
     if (t0 != null)
     {
         t0.Parent = a;
     }
     a.RChild = t1;
     if (t1 != null)
     {
         t1.Parent = a;
     }
     UpdateHeight(a);
     c.LChild = t2;
     if (t2 != null)
     {
         t2.Parent = c;
     }
     c.RChild = t3;
     if (t3 != null)
     {
         t3.Parent = c;
     }
     UpdateHeight(c);
     b.LChild = a;
     a.Parent = b;
     b.RChild = c;
     c.Parent = b;
     UpdateHeight(b);
     return(b);
 }
Пример #18
0
        static void Main(string[] args)
        {
            BinNode <double> tree = new BinNode <double>(1);

            BuildTree(tree);
            Console.WriteLine(TRG23(tree) - TRG23b(tree));
        }
Пример #19
0
        private static void CheckBCountToRoot(RedBlackTree <int> rbTree)
        {
            List <BinNode <int> > list = rbTree.TraverseLevel(rbTree.Root);
            int count = 0;

            for (int i = 0; i < list.Count; ++i)
            {
                BinNode <int> node = list[i];
                if (null != node.LeftChild || null != node.RightChild)
                {
                    continue;
                }
                int bCount = BCountToRoot(node);
                if (count <= 0 && bCount > 0)
                {
                    count = bCount;
                }

                if (count > 0 && bCount != count)
                {
                    Console.WriteLine("CheckBCountToRoot:Error :" + node.Element.ToString());
                }
                Console.WriteLine(node.Element.ToString() + "   " + count);
            }
        }
Пример #20
0
        public static bool What(BinNode <int> pos)
        {
            BinNode <int> left  = FirstLeft(pos);
            BinNode <int> right = FirstRight(pos);

            int sum = left.GetValue() + right.GetValue();

            left  = left.GetRight();
            right = right.GetLeft();

            while ((left != right) && (left.GetRight() != right) &&
                   (left.GetValue() + right.GetValue() != sum))
            {
                left  = left.GetRight();
                right = right.GetLeft();
            }
            if (left == right)
            {
                return(right.GetValue() == sum);
            }

            if (left.GetRight() == right)
            {
                return(left.GetValue() + right.GetValue() == sum);
            }
            return(false);
            // return left.GetValue() + right.GetValue() == sum;
        }
Пример #21
0
        public void ZeroBalanceFactor_1()
        {
            var a = 3;
            var testNode = new BinNode<int>(ref a);

            Assert.AreEqual(0, testNode.BalanceFactor);
        }
Пример #22
0
        void BuildRecursivly(BinNode <List <Triangle3D> > node)
        {
            if (node.data.Count <= 1)
            {
                return;
            }

            Triangle3D main  = node.data[0];
            Plane      plane = new Plane(main.a, main.normal);

            for (int i = 1; i < node.data.Count; i++)
            {
                Triangle3D go = node.data[i];

                float fa = Helper.SnapToZero(plane.Point(go.a));
                float fb = Helper.SnapToZero(plane.Point(go.b));
                float fc = Helper.SnapToZero(plane.Point(go.c));

                if (fa >= 0 && fb >= 0 && fc >= 0)
                {
                    if (node.right == null)
                    {
                        node.right      = new BinNode <List <Triangle3D> >(new List <Triangle3D>());
                        node.right.data = new List <Triangle3D>();
                    }

                    node.right.data.Add(go);
                }
                else if (fa <= 0 && fb <= 0 && fc <= 0)
                {
                    if (node.left == null)
                    {
                        node.left      = new BinNode <List <Triangle3D> >(new List <Triangle3D>());
                        node.left.data = new List <Triangle3D>();
                    }

                    node.left.data.Add(go);
                }
                else
                {
                    List <Triangle3D> ts = Rasterizer.ClipTriangle(go, plane);
                    node.data.AddRange(ts);
                    node.data.RemoveAt(i);
                    i--;
                }
            }

            node.data.RemoveRange(1, node.data.Count - 1);

            if (node.left != null)
            {
                BuildRecursivly(node.left);
            }

            if (node.right != null)
            {
                BuildRecursivly(node.right);
            }
        }
Пример #23
0
 /// <summary>
 /// 将某棵子树作为结点的左孩子
 /// </summary>
 /// <typeparam name="T">类型参数</typeparam>
 /// <param name="p">结点</param>
 /// <param name="rc">要插入的子树</param>
 public static void AttachAsRChild <T>(this BinNode <T> p, BinNode <T> rc) where T : IComparable <T>
 {
     p.RChild = rc;
     if (rc != null)
     {
         rc.Parent = p;
     }
 }
Пример #24
0
 /// <summary>
 /// 将某棵子树作为结点的左孩子
 /// </summary>
 /// <typeparam name="T">类型参数</typeparam>
 /// <param name="p">结点</param>
 /// <param name="lc">要插入的子树</param>
 public static void AttachAsLChild <T>(this BinNode <T> p, BinNode <T> lc) where T : IComparable <T>
 {
     p.LChild = lc;
     if (lc != null)
     {
         lc.Parent = p;
     }
 }
Пример #25
0
 public static BinNode <int> FirstRight(BinNode <int> pos)
 {
     while (pos.GetRight() != null)
     {
         pos = pos.GetRight();
     }
     return(pos);
 }
Пример #26
0
 /// <summary>
 /// Default CTOR
 /// </summary>
 public HuffmanTree()
 {
     root        = null;
     EncodedData = null;
     freqList    = null;
     isNull      = true;
     comprssdLen = 0;
 }
Пример #27
0
 public static int TRG21High(BinNode <int> tree)
 {
     if (tree == null)
     {
         return(0);
     }
     return(1 + Math.Max(TRG21High(tree.GetLeft()), TRG21High(tree.GetRight())));
 }
Пример #28
0
 public static int CountNodes1 <T>(BinNode <T> tr)
 {
     if (tr == null)
     {
         return(0);
     }
     return(1 + CountNodes1(tr.GetLeft()) + CountNodes1(tr.GetRight()));
 }
Пример #29
0
 public static BinNode <int> FirstLeft(BinNode <int> pos)
 {                                 // A: total 10pt
     while (pos.GetLeft() != null) // 4pt
     {
         pos = pos.GetLeft();      // 4pt
     }
     return(pos);                  // 2pt
 }
Пример #30
0
 public BinNode <T> Uncle(BinNode <T> node)
 {
     if (node.ParentNode.IsLChild())
     {
         return(node.ParentNode.ParentNode.RightChild);
     }
     return(node.ParentNode.ParentNode.LeftChild);
 }
Пример #31
0
 public static int TRG27 <T>(BinNode <T> tree) // O(n)
 {
     if (tree == null)
     {
         return(0);
     }
     return(1 + Math.Max(TRG27(tree.GetRight()), TRG27(tree.GetLeft())));
 }