Пример #1
0
        private static BinaryTreeNode Rectify(BinaryTreeNode binaryTreeNode, int value1,
                                              int value2)
        {
            if (binaryTreeNode == null)
            {
                return(null);
            }

            BinaryTreeNode leftNode = Rectify(binaryTreeNode.GetLeftNode(), value1, value2);

            if (previousNode != null &&
                previousNode.GetData() > binaryTreeNode.GetData() &&
                previousNode.GetData() == value1 &&
                value2 < value1)
            {
                binaryTreeNode.SetLeftNode(leftNode);
                binaryTreeNode.SetData(value1);
                previousNode.SetData(value2);
                return(binaryTreeNode);
            }

            previousNode = binaryTreeNode;

            BinaryTreeNode right = Rectify(binaryTreeNode.GetRightNode(), value1, value2);

            binaryTreeNode.SetLeftNode(leftNode);
            binaryTreeNode.SetRightNode(right);
            return(binaryTreeNode);
        }
Пример #2
0
        private static ResultNodes _CorrectBSTUtil(BinaryTreeNode binaryTreeNode, ResultNodes resultNodes)
        {
            if (binaryTreeNode == null)
            {
                return(resultNodes);
            }

            resultNodes = _CorrectBSTUtil(binaryTreeNode.GetLeftNode(), resultNodes);

            if (resultNodes != null &&
                resultNodes.GetPreviousNode() != null &&
                resultNodes.GetPreviousNode().GetData() > binaryTreeNode.GetData())
            {
                if (resultNodes.GetFirstNode() == null)
                {
                    resultNodes.SetFirstNode(resultNodes.GetPreviousNode());
                    resultNodes.SetSecondNode(binaryTreeNode);
                }
                else
                {
                    resultNodes.SetThirdNode(binaryTreeNode);
                }
            }

            if (resultNodes != null)
            {
                resultNodes.SetPreviousNode(binaryTreeNode);
            }
            BSTNodes++;
            return(_CorrectBSTUtil(binaryTreeNode.GetRightNode(), resultNodes));
        }
Пример #3
0
        private static BinaryTreeNode SwapAndRectify(BinaryTreeNode rootNode, BinaryTreeNode node1, BinaryTreeNode node2)
        {
            int value1 = node1.GetData();
            int value2 = node2.GetData();

            rootNode = Rectify(rootNode, value1, value2);
            return(rootNode);
        }
Пример #4
0
 private static int[] GetArrayFromTree(BinaryTreeNode binaryTreeNode, int count)
 {
     BSTNodesArray = new int[BSTNodes];
     if (binaryTreeNode != null)
     {
         GetArrayFromTree(binaryTreeNode.GetLeftNode(), count);
         BSTNodesArray[count++] = binaryTreeNode.GetData();
         GetArrayFromTree(binaryTreeNode.GetRightNode(), count);
     }
     return(BSTNodesArray);
 }
Пример #5
0
        public static void PrintTree(BinaryTreeNode binaryTreeNode)
        {
            if (binaryTreeNode == null)
            {
                return;
            }

            //Inorder Traversal
            PrintTree(binaryTreeNode.GetLeftNode());
            Console.Write(binaryTreeNode.GetData() + "->");
            PrintTree(binaryTreeNode.GetRightNode());
        }