public ArrayList Traverse(Ordering order)
        {
            ArrayList trvlst = new ArrayList();

            SimpleBinaryTreeNode.Traverse(ref trvlst, this, order, null);
            return(trvlst);
        }
Ejemplo n.º 2
0
        public static void tempTV()
        {
            Console.WindowWidth = 240;//240이 최대
            SBTnodeI[] bt = new SBTnodeI[10];
            for (int i = 0; i < 10; i++)
            {
                bt[i] = new SBTnodeI(i);
            }
            bt[0].Left  = bt[1];
            bt[1].Left  = bt[3];
            bt[3].Left  = bt[4];
            bt[3].Right = bt[5];
            bt[0].Right = bt[2];
            bt[2].Left  = bt[6];
            bt[2].Right = bt[7];
            bt[7].Right = bt[8];
            bt[8].Left  = bt[9];

            int             h          = bt[0].Height;
            int             cont_depth = 0;
            int             cont_rel   = 0;
            List <TV_Block> tv         = new List <TV_Block>();

            buildtv(tv, bt[0], 0, 0, ref cont_depth, ref cont_rel);
            foreach (TV_Block e in tv)
            {
                Console.WriteLine("{0} : (depth : {1}), (rel : {2})", e.str, e.depth, e.rel);
            }
        }
Ejemplo n.º 3
0
        }//...책에 있는 방식이 더 간단한데? 애미 ㅋㅋㅋㅋ

        //아래가 책에 나와있는 방식
        static public double EvaluateExpTreeP2(SimpleBinaryTreeNode rtnd)
        {
            double op1 = ((!(rtnd.Left.IsTerminal())) ? EvaluateExpTreeP2(rtnd.Left) : rtnd.Left.Data);
            double op2 = ((!(rtnd.Right.IsTerminal())) ? EvaluateExpTreeP2(rtnd.Right) : rtnd.Right.Data);

            return(((SOpr)(rtnd.Data)).oprt.Invoke(op1, op2));
        }
        public ArrayList Traverse(Ordering order, TraverseEvent act, NodeEventArgs args)//trvlist = traversal list, rtnd = root node.
        {
            ArrayList trvlst = new ArrayList();

            SimpleBinaryTreeNode.Traverse(ref trvlst, this, order, act, args);
            return(trvlst);
        }
Ejemplo n.º 5
0
        public static void SBT_Test8()
        {
            SBTnodeI sbt1 = new SBTnodeI(1);
            SBTnodeI sbt2 = new SBTnodeI(2);
            SBTnodeI sbt3 = new SBTnodeI(3);
            SBTnodeI sbt4 = new SBTnodeI(4);

            sbt1.ChangeLeftSubtree(sbt2);
            sbt1.ChangeRightSubtree(sbt3);
            Console.WriteLine("{0}, {1}", sbt1.Left.Data, sbt1.Right.Data);
            sbt1.ChangeLeftSubtree(null);
            Console.WriteLine(sbt1.IsLeftEmpty());
            sbt1.ChangeLeftSubtree(sbt4);
            sbt3.ChangeRightSubtree(sbt2);
            Console.WriteLine(sbt1.Height);
            Console.WriteLine(sbt3.Height);
            Console.WriteLine(sbt2.Height);
            Console.WriteLine(sbt1.GetHeightDiff());

            /*
             * result :
             * 2, 3
             * True
             * 2
             * 1
             * 0
             * -1
             */
        }
 public void Dispose()
 {
     _data  = null;
     _left  = null;
     _right = null;
     _dtype = null;
 }
Ejemplo n.º 7
0
        public static void SBT_Test4()
        {
            SBTnodeI B = new SBTnodeI(typeof(int), 1);
            SBTnodeI A = new SBTnodeI(typeof(int), 10, B);

            Console.WriteLine(A);
            Console.WriteLine(A.Left);
        }
Ejemplo n.º 8
0
        public static void SBT_Test3()
        {
            SBTnodeI B = new SBTnodeI(typeof(int), 1);
            SBTnodeI A = new SBTnodeI(typeof(int), 10);

            A.Right = B;
            Console.WriteLine(A.Right);
            Console.WriteLine(A.Left);
        }
        public SimpleBinaryTreeNode RemoveLeftSubtree()
        {
            if (IsLeftEmpty())
            {
                throw new SBTNodeEmptyException(false);
            }
            SimpleBinaryTreeNode buf = _left;

            _left = null;
            return(buf);
        }
        public SimpleBinaryTreeNode RemoveRightSubtree()
        {
            if (IsRightEmpty())
            {
                throw new SBTNodeEmptyException(true);
            }
            SimpleBinaryTreeNode buf = _right;

            _right = null;
            return(buf);
        }
Ejemplo n.º 11
0
        public static void SBT_Test6()
        {
            var      intt = typeof(int);
            SBTnodeI nd1  = new SBTnodeI(intt, 1);
            SBTnodeI nd2  = new SBTnodeI(intt, 2);
            SBTnodeI nd3  = new SBTnodeI(intt, 3);
            SBTnodeI nd4  = new SBTnodeI(intt, 4);

            nd1.Left  = nd2;
            nd1.Right = nd3;
            nd2.Left  = nd4;
            ArrayList trvlst;

            trvlst = nd1.Traverse(SBTnodeI.InOrder, (SimpleBinaryTreeNode node) => { Console.WriteLine(node.Data); });
        }
Ejemplo n.º 12
0
        public static void SBT_Test7()
        {
            SBTnodeI nd1 = new SBTnodeI(1, null, null);
            SBTnodeI nd2 = new SBTnodeI(3.5);

            Console.WriteLine("{0}, {1}", nd1.Data, nd1.DataType);
            Console.WriteLine("{0}, {1}", nd2.Data, nd2.DataType);
            Console.WriteLine(nd1.Data + nd2.Data);

            /* result :
             * 1, System.Int32
             * 3.5, System.Double
             * 4.5
             * 계속하려면 아무 키나 누르십시오 . . .
             */
        }
Ejemplo n.º 13
0
        public static void CalTest1()
        {
            string               exp     = "1 + 3 - (6 - 3 *7)/3";
            ArrayList            posfix  = SSCLCLTR.ConvertInfixToPosfixTest(exp);
            SimpleBinaryTreeNode exptree = SSCLCLTR.MakeExpTree(posfix);

            exptree.Traverse(SimpleBinaryTreeNode.PostOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); });
            Console.WriteLine();
            exptree.Traverse(SimpleBinaryTreeNode.InOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); });
            Console.WriteLine();
            exptree.Traverse(SimpleBinaryTreeNode.PreOrder, (SimpleBinaryTreeNode nd) => { Console.Write(nd.Data); });
            Console.WriteLine();
            Console.WriteLine(SSCLCLTR.EvaluateExpTreeP2(exptree));
            Console.WriteLine(SSCLCLTR.EvaluateExpTreeP(exptree));
            //EvaluateExpTreeP에 의해서 exptree가 수정되기 때문에 P 쓰고 P2 쓰면 에러가 났던 것.
            //exptree가 수정되지 않는 EvaluateExpTreeP를 생각해봐야...
            Console.WriteLine(1 + 3 - (6 - 3 * 7) / 3);
        }
Ejemplo n.º 14
0
        public static void SBT_Test5()
        {
            var      intt = typeof(int);
            SBTnodeI nd1  = new SBTnodeI(intt, 1);
            SBTnodeI nd2  = new SBTnodeI(intt, 2);
            SBTnodeI nd3  = new SBTnodeI(intt, 3);
            SBTnodeI nd4  = new SBTnodeI(intt, 4);

            nd1.Left  = nd2;
            nd1.Right = nd3;
            nd2.Left  = nd4;
            ArrayList trvlst;

            trvlst = nd1.Traverse(SBTnodeI.InOrder);
            foreach (int e in trvlst)
            {
                Console.WriteLine(e);
            }
        }
Ejemplo n.º 15
0
        static public double EvaluateExpTreeP(SimpleBinaryTreeNode rtnd)
        {
            var       numbuf  = new SimpleLinkedListBasedStack <double>();
            ArrayList trvlist = new ArrayList();

            SimpleBinaryTreeNode.Traverse(ref trvlist, rtnd, SimpleBinaryTreeNode.PostOrder,
                                          (SimpleBinaryTreeNode nd) => {
                if ((nd.Data is SOpr) && (nd.Left.DataType == typeof(double)) && (nd.Right.DataType == typeof(double)))
                {
                    double a = (double)trvlist[trvlist.Count - 2];  //stack 쓰는게 더 적절하지 않았겠냐..? 아 trvlist의 작성이...
                    double b = (double)trvlist[trvlist.Count - 1];
                    double r = ((SOpr)(nd.Data)).oprt.Invoke(a, b);
                    nd.Set(r);
                    trvlist.RemoveAt(trvlist.Count - 1);
                    trvlist.RemoveAt(trvlist.Count - 1);
                }
            });
            return((double)trvlist[0]);
        }//...책에 있는 방식이 더 간단한데? 애미 ㅋㅋㅋㅋ
Ejemplo n.º 16
0
        public static void SBT_Test1()
        {
            SBTnodeI A = new SBTnodeI(typeof(int), 10);

            //Console.WriteLine(A.Left);
            Console.WriteLine(A.Data);
            Console.WriteLine(A.DataType);
            Console.WriteLine(A.Data.GetType());
            A.Data = 30;
            Console.WriteLine(A.Data);
            //A.Data = 30.5;
            //Console.WriteLine(A.Data); 캐스팅오류 제대로 발생해주시구연~~~~~~~
            Console.WriteLine("{0}, {1}, {2}", A.IsLeftEmpty(), A.IsRightEmpty(), A.IsTerminal());
            A.Left = new SBTnodeI(typeof(double), 20.5);
            Console.WriteLine("{0}, {1}, {2}", A.IsLeftEmpty(), A.IsRightEmpty(), A.IsTerminal());
            A.Right = new SBTnodeI(typeof(string), "퍄퍄");
            Console.WriteLine("{0}, {1}, {2}", A.IsLeftEmpty(), A.IsRightEmpty(), A.IsTerminal());
            Console.WriteLine(A.Left.Data.GetType());
            Console.WriteLine(A.Right.Data.GetType());
            //A.Right.Data = 20; 기대한대로 캐스팅오류 제대로 발생해주시구연~~~~~~
        }
Ejemplo n.º 17
0
        public static void buildtv(List <TV_Block> tv, SBTnodeI nd, int depth, int rel, ref int cont_depth, ref int cont_rel)//rel = right edge length.
        {
            if (cont_depth > depth)
            {
                tv.Add(new TV_Block(String.Format("({0})", nd.Data.ToString()), depth, ++cont_rel));
            }
            else
            {
                tv.Add(new TV_Block(String.Format("({0})", nd.Data.ToString()), depth, rel));
                cont_rel = rel;
            }
            cont_depth = depth;

            if (!(nd.IsLeftEmpty()))
            {
                buildtv(tv, nd.Left, depth + 1, cont_rel, ref cont_depth, ref cont_rel);
            }

            if (!(nd.IsRightEmpty()))
            {
                buildtv(tv, nd.Right, depth + 1, cont_rel + 1, ref cont_depth, ref cont_rel);
            }
        }
 public SimpleBinaryTreeNode(Type type, object data, SimpleBinaryTreeNode left, SimpleBinaryTreeNode right)
 {
     _dtype = type; _data = data; _left = left; _right = right;
 }
 public void ChangeRightSubtree(SimpleBinaryTreeNode substitute)
 {
     //if (IsRightEmpty()) throw new SBTNodeEmptyException(true);
     _right = substitute;
 }
 public void ChangeLeftSubtree(SimpleBinaryTreeNode substitute)
 {
     //if (IsLeftEmpty()) throw new SBTNodeEmptyException(false);
     _left = substitute;
 }
 public SimpleBinaryTreeNode(dynamic data, SimpleBinaryTreeNode left, SimpleBinaryTreeNode right) : this((Type)data.GetType(), (object)data, left, right)
 {
 }
 public SimpleBinaryTreeNode(Type type, object data, SimpleBinaryTreeNode left) : this(type, data, left, null)
 {
 }
 public static void Traverse(ref ArrayList trvlst, SimpleBinaryTreeNode rtnd, Ordering order)
 {
     order.Invoke(ref trvlst, rtnd, order, null, null);
 }
 public static void Traverse(ref ArrayList trvlst, SimpleBinaryTreeNode rtnd, Ordering order, OrderAction act)
 {
     order.Invoke(ref trvlst, rtnd, order, (SimpleBinaryTreeNode nd, NodeEventArgs args) => { act.Invoke(nd); }, null);
 }
 public static void Traverse(ref ArrayList trvlst, SimpleBinaryTreeNode rtnd, Ordering order, TraverseEvent act, NodeEventArgs args)//trvlist = traversal list, rtnd = root node.
 {
     order.Invoke(ref trvlst, rtnd, order, act, args);
 }