using
System;
using
System.Collections.Generic;
class
Node {
public
int
key;
public
List<Node> child;
public
Node(
int
data) {
key = data;
child =
new
List<Node>();
}
}
class
GFG {
static
void
immediateSmallerElementUtil(Node root,
int
x, Node[] res) {
if
(root ==
null
)
return
;
if
(root.key < x) {
if
(res[0] ==
null
|| res[0].key < root.key) {
res[0] = root;
}
}
int
numChildren = root.child.Count;
for
(
int
i = 0; i < numChildren; i++) {
immediateSmallerElementUtil(root.child[i], x, res);
}
}
static
Node immediateSmallerElement(Node root,
int
x) {
Node[] res =
new
Node[1];
immediateSmallerElementUtil(root, x, res);
return
res[0];
}
public
static
void
Main() {
Node root =
new
Node(20);
root.child.Add(
new
Node(2));
root.child.Add(
new
Node(34));
root.child.Add(
new
Node(50));
root.child.Add(
new
Node(60));
root.child.Add(
new
Node(70));
root.child[0].child.Add(
new
Node(15));
root.child[0].child.Add(
new
Node(20));
root.child[1].child.Add(
new
Node(30));
root.child[2].child.Add(
new
Node(40));
root.child[2].child.Add(
new
Node(100));
root.child[2].child.Add(
new
Node(20));
root.child[0].child[1].child.Add(
new
Node(25));
root.child[0].child[1].child.Add(
new
Node(50));
int
x = 30;
Console.Write(
"Immediate smaller element of "
+ x +
" is "
);
Console.WriteLine(immediateSmallerElement(root, x).key);
}
}