public bool CheckBST(BSTNode <int> root) { FindAnElementInBST findNode = new FindAnElementInBST(); if (root == null) { return(true); } if (root.Left != null && root.Data < findNode.FindMaximum(root.Left).Data) { return(false); } if (root.Right != null && root.Data > findNode.FindMinimum(root.Right).Data) { return(false); } if (!CheckBST(root.Left) || !CheckBST(root.Right)) { return(false); } return(true);; }
public BSTNode <int> DeleteNode(BSTNode <int> root, BSTNode <int> node) { BSTNode <int> temp; if (root == null) { Console.Write("Error!!"); } else if (root.Data > node.Data) { root.Left = DeleteNode(root.Left, node); } else if (root.Data < node.Data) { root.Right = DeleteNode(root.Right, node); } else { //Found Data to delete if (root.Left != null && root.Right != null) { //Get max node in left sub tree FindAnElementInBST findElement = new FindAnElementInBST(); temp = findElement.FindMaximum(root.Left); root.SetData(temp.Data); DeleteNode(root.Left, temp); } else { //One child temp = root; if (root.Left == null) { root = root.Right; } else if (root.Right == null) { root = root.Left; } } } return(root); }