コード例 #1
0
ファイル: BinaryTree.cs プロジェクト: raj-vel/MyIntPrep
 public BTNodeWithSibling(int val)
 {
     this.data = val;
     left      = null;
     right     = null;
     next      = null;
 }
コード例 #2
0
ファイル: BinaryTree.cs プロジェクト: raj-vel/MyIntPrep
        private BTNodeWithSibling GetNextSibiling(BTNodeWithSibling curr, bool isLeft)
        {
            if (curr == null)
            {
                return(curr);
            }
            if (isLeft)
            {
                if (curr.right != null)
                {
                    return(curr.right);
                }
                else if (curr.next != null && curr.next.left != null)
                {
                    return(curr.next.left);
                }
                else if (curr.next != null && curr.next.right != null)
                {
                    return(curr.next.right);
                }
            }
            else
            {
                if (curr.next != null && curr.next.left != null)
                {
                    return(curr.next.left);
                }
                else if (curr.next != null && curr.next.right != null)
                {
                    return(curr.next.right);
                }
            }

            return(null);
        }
コード例 #3
0
ファイル: BinaryTree.cs プロジェクト: raj-vel/MyIntPrep
        private BTNodeWithSibling ConnectSibilings(BTNodeWithSibling node)
        {
            if (node == null)
            {
                return(null);
            }

            Queue <BTNodeWithSibling> queue = new Queue <BTNodeWithSibling>();

            queue.Enqueue(node);
            queue.Enqueue(null);

            while (queue.Count > 0)
            {
                var curr = queue.Dequeue();

                if (curr != null && curr.left != null)
                {
                    curr.left.next = GetNextSibiling(curr, true);
                }
                if (curr != null && curr.right != null)
                {
                    curr.right.next = GetNextSibiling(curr, false);
                }

                if (curr == null && queue.Count > 0)
                {
                    queue.Enqueue(curr);
                }

                if (curr != null && curr.left != null)
                {
                    queue.Enqueue(curr.left);
                }
                if (curr != null && curr.right != null)
                {
                    queue.Enqueue(curr.right);
                }
            }
            return(node);
        }
コード例 #4
0
ファイル: BinaryTree.cs プロジェクト: raj-vel/MyIntPrep
        private BTNodeWithSibling ConstructBTNodeWithSiblingData_1()
        {
            BTNodeWithSibling node;

            node = new BTNodeWithSibling(1);

            node.left  = new BTNodeWithSibling(2);
            node.right = new BTNodeWithSibling(3);

            node.left.left   = new BTNodeWithSibling(4);
            node.left.right  = new BTNodeWithSibling(5);
            node.right.left  = new BTNodeWithSibling(6);
            node.right.right = new BTNodeWithSibling(7);


            //node.left.left = new BTNode(3);
            //node.left.right = new BTNode(4);
            //node.right = new BTNode(2);
            //node.right.left = new BTNode(4);
            //node.right.right = new BTNode(3);
            return(node);
        }