using
System;
using
System.Collections;
using
System.Collections.Generic;
public
class
Node {
public
int
data;
public
int
hd;
public
Node left, right;
public
Node(
int
key)
{
data = key;
hd = Int32.MaxValue;
left = right =
null
;
}
}
public
class
BinaryTree {
public
static
void
printBottomViewUtil(
Node root,
int
curr,
int
hd,
SortedDictionary<
int
, Tuple<
int
,
int
> > m)
{
if
(root ==
null
)
return
;
if
(!m.ContainsKey(hd)) {
m[hd] = Tuple.Create(root.data, curr);
}
else
{
Tuple<
int
,
int
> p = m[hd];
if
(p.Item2 <= curr) {
p = Tuple.Create(root.data, curr);
}
m[hd] = p;
}
printBottomViewUtil(root.left, curr + 1, hd - 1, m);
printBottomViewUtil(root.right, curr + 1, hd + 1,
m);
}
public
static
void
printBottomView(Node root)
{
SortedDictionary<
int
, Tuple<
int
,
int
> > m
=
new
SortedDictionary<
int
, Tuple<
int
,
int
> >();
printBottomViewUtil(root, 0, 0, m);
foreach
(KeyValuePair<
int
, Tuple<
int
,
int
> > it
in
m)
{
Tuple<
int
,
int
> p = it.Value;
Console.Write(p.Item1 +
" "
);
}
}
public
static
void
Main(
string
[] args)
{
Node root =
new
Node(20);
root.left =
new
Node(8);
root.right =
new
Node(22);
root.left.left =
new
Node(5);
root.left.right =
new
Node(3);
root.right.left =
new
Node(4);
root.right.right =
new
Node(25);
root.left.right.left =
new
Node(10);
root.left.right.right =
new
Node(14);
Console.WriteLine(
"Bottom view of the given binary tree :"
);
printBottomView(root);
}
}