using
System;
class
GFG {
public
class
BSTnode {
public
int
data;
public
BSTnode left, right;
public
BSTnode(
int
data)
{
this
.data = data;
this
.left =
this
.right =
null
;
}
}
static
BSTnode leftMost(BSTnode root)
{
if
(root ==
null
)
return
null
;
while
(root.left !=
null
)
root = root.left;
return
root;
}
static
BSTnode deleteNode(BSTnode root)
{
if
(root.left ==
null
) {
BSTnode child = root.right;
root =
null
;
return
child;
}
else
if
(root.right ==
null
) {
BSTnode child = root.left;
root =
null
;
return
child;
}
BSTnode next = leftMost(root.right);
root.data = next.data;
root.right = deleteNode(root.right);
return
root;
}
static
BSTnode removeRange(BSTnode node,
int
low,
int
high)
{
if
(node ==
null
)
return
null
;
node.left = removeRange(node.left, low, high);
node.right = removeRange(node.right, low, high);
if
(node.data >= low && node.data <= high)
return
deleteNode(node);
return
node;
}
static
void
inorder(BSTnode root)
{
if
(root !=
null
) {
inorder(root.left);
Console.Write(root.data +
" "
);
inorder(root.right);
}
}
public
static
void
Main(String[] args)
{
BSTnode root =
new
BSTnode(50);
root.left =
new
BSTnode(30);
root.right =
new
BSTnode(70);
root.left.right =
new
BSTnode(40);
root.right.right =
new
BSTnode(80);
root.right.left =
new
BSTnode(60);
root.left.left =
new
BSTnode(20);
Console.Write(
"Inorder Before deletion: "
);
inorder(root);
root = removeRange(root, 50, 70);
Console.Write(
"\nInorder After deletion: "
);
inorder(root);
}
}