class Max{
int max_level ;
}
class Print{
//Max max_level = new Max();
int max_level = 0 ;
// left and right view can be obtained using queue ( level order traversals )
void
leftViewUtil(Node node,
int
level)
{
// Base Case
if
(node==
null
)
return
;
// If this is the first node of its level
if
(max_level < level)
{
// print first max level in from left side.
System.out.print(
" "
+ node.data);
max_level = level;
}
// Recur for left and right subtrees
leftViewUtil(node.left, level+
1
);
leftViewUtil(node.right, level+
1
);
}
void rightViewUtil(Node node, int level) {
if(node == null) return;
if(max_level < level) {
sysout(node.data);
max_level = level;
}
rightViewUtil(node.right, level+1);
rightViewUtil(node.left, level+1);
}
BottomView / TopView : -
// Java Program to print Bottom View of Binary Tree
import
java.util.*;
import
java.util.Map.Entry;
// Tree node class
class
Node
{
int
data;
//data of the node
int
hd;
//horizontal distance of the node
Node left, right;
//left and right references
// Constructor of tree node
public
Node(
int
key)
{
data = key;
hd = Integer.MAX_VALUE;
left = right =
null
;
}
}
//Tree class
class
Tree
{
Node root;
//root node of tree
// Default constructor
public
Tree() {}
// Parameterized tree constructor
public
Tree(Node node)
{
root = node;
}
// Method that prints the bottom view.
public
void
bottomView()
{
if
(root ==
null
)
return
;
// Initialize a variable 'hd' with 0 for the root element.
int
hd =
0
;
// TreeMap which stores key value pair sorted on key value
Map map =
new
TreeMap<>();
// Queue to store tree nodes in level order traversal
Queue queue =
new
LinkedList();
// Assign initialized horizontal distance value to root
// node and add it to the queue.
root.hd = hd;
queue.add(root);
// Loop until the queue is empty (standard level order loop)
while
(!queue.isEmpty())
{
Node temp = queue.remove();
// Extract the horizontal distance value from the
// dequeued tree node.
hd = temp.hd;
// Put the dequeued tree node to TreeMap having key
// as horizontal distance. Every time we find a node
// having same horizontal distance we need to replace
// the data in the map.
map.put(hd, temp.data);
// If the dequeued node has a left child add it to the
// queue with a horizontal distance hd-1.
if
(temp.left !=
null
)
{
temp.left.hd = hd-
1
;
queue.add(temp.left);
}
// If the dequeued node has a left child add it to the
// queue with a horizontal distance hd+1.
if
(temp.right !=
null
)
{
temp.right.hd = hd+
1
;
queue.add(temp.right);
}
}
// Extract the entries of map into a set to traverse
// an iterator over that.
Set> set = map.entrySet();
// Make an iterator
Iterator> iterator = set.iterator();
// Traverse the map elements using the iterator.
while
(iterator.hasNext())
{
Map.Entry me = iterator.next();
System.out.print(me.getValue()+
" "
);
}
}
}
// Main driver class
public
class
BottomView
{
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
);
Tree tree =
new
Tree(root);
System.out.println(
"Bottom view of the given binary tree:"
);
tree.bottomView();
}
}
Vertical sum of Binary Tree:
// Method to be called by the consumer classes
// like Main class
public
void
VerticalSumMain() { VerticalSum(root); }
// A wrapper over VerticalSumUtil()
private
void
VerticalSum(TreeNode root) {
// base case
if
(root ==
null
) {
return
; }
// Creates an empty hashMap hM
HashMap hM =
new
HashMap();
// Calls the VerticalSumUtil() to store the
// vertical sum values in hM
VerticalSumUtil(root,
0
, hM);
// Prints the values stored by VerticalSumUtil()
if
(hM !=
null
) {
System.out.println(hM.entrySet());
}
}
// Traverses the tree in Inoorder form and builds
// a hashMap hM that contains the vertical sum
private
void
VerticalSumUtil(TreeNode root,
int
hD,
HashMap hM) {
// base case
if
(root ==
null
) {
return
; }
// Store the values in hM for left subtree
VerticalSumUtil(root.left(), hD -
1
, hM);
// Update vertical sum for hD of this node
int
prevSum = (hM.get(hD) ==
null
) ?
0
: hM.get(hD);
hM.put(hD, prevSum + root.key());
// Store the values in hM for right subtree
VerticalSumUtil(root.right(), hD +
1
, hM);
}
x
No comments:
Post a Comment