private void button7_Click(object sender, EventArgs e)
        {
            //Construct a binary tree as the given example
            ////////////4/////////
            ///////2 ////////5///
            ////1////3/////////6
            //0////////////////////

            //Expected output as a doubly linked list  0,1,2,3,4,5,6

            //BiNode root = new BiNode(4);

            //root.Node1 = new BiNode(2);
            //root.Node2 = new BiNode(5);

            //root.Node1.Node1 = new BiNode(1);
            //root.Node1.Node2 = new BiNode(3);
            //root.Node2.Node1 = null;
            //root.Node2.Node2 = new BiNode(6);

            //root.Node1.Node1.Node1 = new BiNode(0);

            //test case as in the picture

            BiNode root = new BiNode(1);

            root.Node1 = new BiNode(2);
            root.Node2 = new BiNode(3);

            root.Node1.Node1 = new BiNode(4);
            root.Node1.Node2 = new BiNode(5);
            root.Node2.Node1 = new BiNode(6);
            root.Node2.Node2 = new BiNode(7);

            BinaryTreeToLinkedList obj = new BinaryTreeToLinkedList();


            string output = obj.ConvertWithoutUsingAdditionDataStructure(root);

            this.textBox6.Text = output;
        }
예제 #2
0
 public BiNode()
 {
     this.data       = default(T);
     this.leftChild  = null;
     this.rightChild = null;
 }
예제 #3
0
 public BiNode(T data)
 {
     this.data       = data;
     this.leftChild  = null;
     this.rightChild = null;
 }
예제 #4
0
 public BiNode(BiNode <T> leftBiNode, BiNode <T> rightBiNode)
 {
     this.data       = default(T);
     this.leftChild  = leftBiNode;
     this.rightChild = rightBiNode;
 }
예제 #5
0
 public BiNode(T data, BiNode <T> leftBiNode, BiNode <T> rightBiNode)
 {
     this.data       = data;
     this.leftChild  = leftBiNode;
     this.rightChild = rightBiNode;
 }
예제 #6
0
        private void TryToLoadBiTree()
        {
            List <BiNode> biNodeList     = new List <BiNode>();
            string        biTreeFilePath = FileMg.AutoRDataFileDir + Constants.ROutputFileName;

            if (File.Exists(biTreeFilePath))
            {
                StreamReader sr = new StreamReader(biTreeFilePath);
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine().Trim();

                    string[] attrs = line.Split(' ');
                    if (attrs.Length >= 2)
                    {
                        BiNode leftNode  = new BiNode();
                        BiNode rightNode = new BiNode();
                        BiNode curNode   = new BiNode();

                        string leftValue  = attrs[0];
                        string rightValue = attrs[attrs.Length - 1];

                        if (leftValue.StartsWith("-"))
                        {
                            leftNode.Left  = null;
                            leftNode.Right = null;
                            leftNode.Value = Int32.Parse(leftValue.Substring(1));
                        }
                        else
                        {
                            int pos = Int32.Parse(leftValue);
                            leftNode = biNodeList[pos - 1];
                        }

                        if (rightValue.StartsWith("-"))
                        {
                            rightNode.Left  = null;
                            rightNode.Right = null;
                            rightNode.Value = Int32.Parse(rightValue.Substring(1));
                        }
                        else
                        {
                            int pos = Int32.Parse(rightValue);
                            rightNode = biNodeList[pos - 1];
                        }

                        curNode.Left  = leftNode;
                        curNode.Right = rightNode;
                        curNode.Value = -1;

                        biNodeList.Add(curNode);
                    }
                }

                sr.Close();
            }

            if (biNodeList.Count > 0)
            {
                mBiTreeRoot = biNodeList[biNodeList.Count - 1];
            }
        }
예제 #7
0
 private void Concat(BiNode x, BiNode y)
 {
     x.Node2 = y;
     y.Node1 = x;
 }