using
System;
using
System.Collections.Generic;
class
GFG
{
class
node
{
public
int
data;
public
node left, right;
};
static
node newNode(
int
data)
{
node node =
new
node();
node.data = data;
node.left = node.right =
null
;
return
(node);
}
static
bool
iterativeSearch(node root,
int
x)
{
if
(root ==
null
)
return
false
;
Stack<node> nodeStack =
new
Stack<node>();
nodeStack.Push(root);
while
(nodeStack.Count != 0)
{
node node = nodeStack.Peek();
if
(node.data == x)
return
true
;
nodeStack.Pop();
if
(node.right !=
null
)
nodeStack.Push(node.right);
if
(node.left !=
null
)
nodeStack.Push(node.left);
}
return
false
;
}
public
static
void
Main(String[] args)
{
node root = newNode(2);
root.left = newNode(7);
root.right = newNode(5);
root.left.right = newNode(6);
root.left.right.left = newNode(1);
root.left.right.right = newNode(11);
root.right.right = newNode(9);
root.right.right.left = newNode(4);
if
(iterativeSearch(root, 6))
Console.WriteLine(
"Found"
);
else
Console.WriteLine(
"Not Found"
);
if
(iterativeSearch(root, 12))
Console.WriteLine(
"Found"
);
else
Console.WriteLine(
"Not Found"
);
}
}