/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
class Tree
{
public static void main
(String args
[]) {
int arr[] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
};
TreeImpl tree = new TreeImpl();
tree.insert(100);
tree.insert(50);
tree.insert(60);
tree.insert(150);
tree.insert(10);
tree.insert(5);
tree.insert(140);
tree.insert(200);
tree.insert(210);
tree.inorderTraversal();
tree.removeHalfNodes(tree.root, null, null);
tree.inorderTraversal();
}
}
class TreeImpl
{
public Node root;
Node curr;
public boolean isHalfNode(Node root)
{
if (root == null)
return false;
if ((root.left != null && root.right == null) || (root.left == null && root.right != null))
{
return true;
}
return false;
}
public void removeHalfNodes
(Node root, Node parent,
Boolean isLeft
) {
while (isHalfNode(root))
{
if (root.left != null && root.right == null)
{
if (isLeft)
{
parent.left = root.left;
}
else
{
parent.right = root.left;
}
root = root.left;
}
if (root.left == null && root.right != null)
{
if (isLeft)
{
parent.left = root.right;
}
else
{
parent.right = root.right;
}
root = root.right;
}
}
if (root == null)
return;
removeHalfNodes(root.left, root, true);
removeHalfNodes(root.right, root, false);
}
class Node
{
private Node right;
private Node left;
public Node()
{
}
{
this.data = data;
}
}
{
if (root == null)
{
root = new Node(data);
return;
}
Node temp1 = root, temp2 = null;
boolean left = false, right = false;
while (temp1 != null)
{
temp2 = temp1;
left = false;
right = false;
if (temp1.data.compareTo(data) > 0)
{
left = true;
temp1 = temp1.left;
}
else
{
right = true;
temp1 = temp1.right;
}
}
if (left)
{
temp2.left = new Node(data);
}
else if (right)
{
temp2.right = new Node(data);
}
}
public void putDataInTree
(Integer data, Node root
) {
if (root != null)
{
if (root.data.compareTo(data) > 0)
{
putDataInTree(data, root.left);
}
else
{
putDataInTree(data, root.right);
}
}
else
{
root = new Node(data);
}
}
public void inorderTraversal()
{
inorderTraversal(root, 0);
}
public void inorderTraversal(Node root, int dis)
{
if (root != null)
{
inorderTraversal(root.left, dis + 1);
Integer sumTillNow
= sumMap.
get(dis
); if (sumTillNow == null)
sumTillNow = 0;
sumTillNow = sumTillNow + root.data;
sumMap.put(dis, sumTillNow);
System.
out.
print(root.
data + " "); inorderTraversal(root.right, dis);
}
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KCmltcG9ydCBqYXZhLnV0aWwuQXJyYXlMaXN0OwppbXBvcnQgamF2YS51dGlsLkFycmF5czsKaW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLkl0ZXJhdG9yOwppbXBvcnQgamF2YS51dGlsLkxpbmtlZEhhc2hNYXA7CmltcG9ydCBqYXZhLnV0aWwuTGlzdDsKaW1wb3J0IGphdmEudXRpbC5MaXN0SXRlcmF0b3I7CmltcG9ydCBqYXZhLnV0aWwuTWFwOwoKY2xhc3MgVHJlZQp7CiAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nIGFyZ3NbXSkKICAgIHRocm93cyBFeGNlcHRpb24KICB7CiAgICBpbnQgYXJyW10gPQogICAgewogICAgICAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5LCAxMCwgMTEsIDEyLCAxMywgMTQKICAgIH07CiAgICBUcmVlSW1wbCB0cmVlID0gbmV3IFRyZWVJbXBsKCk7CiAgICB0cmVlLmluc2VydCgxMDApOwogICAgdHJlZS5pbnNlcnQoNTApOwogICAgdH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aWdodCA9IHJvb3QubGVmdDsKICAgICAgICB9CiAgICAgICAgcm9vdCA9IHJvb3QubGVmdDsKICAgICAgfQogICAgICBpZiAocm9vdC5sZWZ0ID09IG51bGwgJiYgcm9vdC5yaWdodCAhPSBudWxsKQogICAgICB7CiAgICAgICAgaWYgKGlzTGVmdCkKICAgICAgICB7CiAgICAgICAgICBwYXJlbnQubGVmdCA9IHJvb3QucmlnaHQ7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICBwYXJlbnQucmlnaHQgPSByb290LnJpZ2h0OwogICAgICAgIH0KICAgICAgICByb290ID0gcm9vdC5yaWdodDsKICAgICAgfQogICAgfQogICAgaWYgKHJvb3QgPT0gbnVsbCkKICAgICAgcmV0dXJuOwogICAgcmVtb3ZlSGFsZk5vZGVzKHJvb3QubGVmdCwgcm9vdCwgdHJ1ZSk7CiAgICByZW1vdmVIYWxmTm9kZXMocm9vdC5yaWdodCwgcm9vdCwgZmFsc2UpOwoKICB9CgogIGNsYXNzIE5vZGUKICB7CiAgICBwcml2YXRlIEludGVnZXIgZGF0YTsKICAgIHByaXZhdGUgTm9kZSByaWdodDsKICAgIHByaXZhdGUgTm9kZSBsZWZ0OwoKICAgIHB1YmxpYyBOb2RlKCkKICAgIHsKICAgIH0KCiAgICBwdWJsaWMgTm9kZShJbnRlZ2VyIGRhdGEpCiAgICB7CiAgICAgIHRoaXMuZGF0YSA9IGRhdGE7CiAgICB9CiAgfQoKCiAgcHVibGljIHZvaWQgaW5zZXJ0KEludGVnZXIgZGF0YSkKICB7CiAgICBpZiAocm9vdCA9PSBudWxsKQogICAgewogICAgICByb290ID0gbmV3IE5vZGUoZGF0YSk7CiAgICAgIHJldHVybjsKICAgIH0KICAgIE5vZGUgdGVtcDEgPSByb290LCB0ZW1wMiA9IG51bGw7CiAgICBib29sZWFuIGxlZnQgPSBmYWxzZSwgcmlnaHQgPSBmYWxzZTsKICAgIHdoaWxlICh0ZW1wMSAhPSBudWxsK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