Exemple #1
0
 public Node(Node l, Node r, Node p, string q)
 {
     left    =   l;
     right   =   r;
     parent  =   p;
     Question =  q;
 }
Exemple #2
0
        public static BinaryTree Load()
        {
            BinaryTree bt = null;

            try
            {

                StreamReader sr = new StreamReader("data");

                string all = sr.ReadToEnd();

                Console.WriteLine(all);

                Node r = Parse( ref all);

                bt = new BinaryTree(r);

                Console.WriteLine("Tree Info:");
                string s = "";
                bt.Info(bt.Root,ref s);
                Console.WriteLine(s);

            }

            catch (Exception ex)
            {
                Console.WriteLine("Загрузка из файла не получилась, создаем дерево вручную из заготовки");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.Source);
                Console.WriteLine(ex.StackTrace);

                #region Стандартное дерево
                Node

                n1 = null,
                n2 = null,
                n3 = null,
                n4 = null,
                n5 = null,
                n6 = null,

                n7 = null;

                n1 = new Node(null, null, n5, "GGG");
                n2 = new Node(null, null, n5, "FFF");
                n3 = new Node(null, null, n6, "EEE");
                n4 = new Node(null, null, n6, "DDD");
                n5 = new Node(n1, n2, n7, "CCC");
                n6 = new Node(n3, n4, n7, "BBB");

                n7 = new Node(n6, n5, null, "AAA");

                n1.Parent = n5;
                n2.Parent = n5;
                n3.Parent = n6;
                n4.Parent = n6;
                n5.Parent = n7;
                n6.Parent = n7;

                bt = new BinaryTree(n7);
                #endregion

            }
            return bt;
        }
Exemple #3
0
        static void Walk(Node node,ref string s)
        {
            //s = "";
            if (node != null)
            {
                ChildFlags cf = ChildFlags.None;
                if (node.Left != null) { cf = cf | ChildFlags.Left; }

                //if (node.Left != null) cfn = 1;

                if (node.Right != null) cf = cf | ChildFlags.Right;

                //if (node.Right != null) cfn = 2;

                //if (node.Right != null && node.Left !=null) cfn = 3;

                s += node.Question + "\n";
                s +=(int)cf + "\n";
                //s += cfn + "\n";

                Console.WriteLine(node.Question);
                Console.WriteLine(cf);

                Walk(node.Left, ref s);
                Walk(node.Right,ref s);
            }
        }
Exemple #4
0
        static Node Parse(ref string all)
        {
            Node qn = null;
            //Console.WriteLine(("|" + all + "|"));
            if (all != "")
            {

                string q, cs;

                // q - question
                // cs - child string

                q = all.Split('\n')[0];

                qn = new Node(q);

                if (myStack.Count == 0)
                {
                    qn.Parent = null;
                }
                else
                qn.Parent = myStack.Pop();

                Console.WriteLine(q);
                cs = all.Split('\n')[1];

                all = all.Replace(q, "");
                all = all.Substring(1,all.Length-1);
                all = all.Substring(1,all.Length-1);

                all = all.Substring(1, all.Length-1);

                ChildFlags cf = (ChildFlags)int.Parse(cs);
                Console.WriteLine("\tcf = {0}",cf);
                //qn.Parent = parent;
                if ((cf & ChildFlags.Left) == ChildFlags.Left)
                {
                    //stack.Push(qn);
                    myStack.Push(qn);

                    qn.Left = Parse(ref all);
                    Console.WriteLine("\tLeft {0}",qn.Left);
                    //Node t = myStack.Pop();
                    //qn.Parent = myStack.Pop();
                    //myStack.Push(t);
                    //stack.Pop();
                    //Console.WriteLine("\tParent {0}",qn.Parent);
                }

                if ((cf & ChildFlags.Right) == ChildFlags.Right)
                {
                    //stack.Push(qn);
                    myStack.Push(qn);

                    qn.Right = Parse(ref all);

                    Console.Write("\tRight {0}", qn.Right);

                    //Node t = myStack.Pop();
                    //qn.Parent = myStack.Pop();
                    //myStack.Push(t);
                    //stack.Pop();
                    //Console.Write("\tParent {0}", qn.Parent);
                }

                if ((cf) == ChildFlags.None)
                {
                    //if (myStack.Count!=0) myStack.Pop();
                }

            }

            return qn;
        }
Exemple #5
0
        public void AddQA(Node n, string q,string a,bool left)
        {
            Console.WriteLine(left);
            Node qn = new Node(q);

                qn.Parent = n.Parent;
                qn.Right = n;
                Node an = new Node(a);
                an.Parent = qn;
                qn.Left = an;

                if (left)
                    n.Parent.Left = qn;
                else
                    n.Parent.Right = qn;
                n.Parent = qn;

            tv.Redraw();
        }
Exemple #6
0
 public BinaryTree(Node r)
 {
     root = r;
     current = r;
 }
Exemple #7
0
        public void Info(Node n,ref string s)
        {
            if (n.Left != null)
            {
                Info(n.Left, ref s);
            }
            if (n.Right != null)
            {
                Info(n.Right, ref s);
            }

            s = "\n" + n.Question + s;
            s = String.Format("\n Parent{0}", n.Parent)+s;
            s = String.Format("\n Left:{0} Right:{1}", n.Left, n.Right)+s;
        }