using
System;
public
class
GFG {
public
class
node {
public
int
data;
public
int
height;
public
node right;
public
node left;
}
static
int
indicator = 0;
static
void
traverse(node head) {
if
(head !=
null
) {
traverse(head.left);
Console.Write(
" data = {0}"
, head.data);
Console.Write(
" height = {0}\n"
, head.height);
traverse(head.right);
}
}
static
void
left_insert(node head, node temp) {
node child =
null
;
if
(head.data > temp.data) {
if
(head.left ==
null
) {
indicator = 1;
child = head.left = temp;
}
else
{
left_insert(head.left, temp);
child = head.left;
}
}
else
{
right_insert(head, temp);
}
if
((indicator == 1) && (child !=
null
)) {
if
(head.height > child.height) {
indicator = 0;
}
else
{
head.height += 1;
}
}
}
static
void
right_insert(node head, node temp) {
node child =
null
;
if
(head.data < temp.data) {
if
(head.right ==
null
) {
indicator = 1;
child = head.right = temp;
}
else
{
right_insert(head.right, temp);
child = head.right;
}
}
else
{
left_insert(head, temp);
}
if
((indicator == 1) && (child !=
null
)) {
if
(head.height > child.height) {
indicator = 0;
}
else
{
head.height += 1;
}
}
}
static
node add_nodes(node head,
int
value) {
node temp_head = head, temp;
if
(head ==
null
) {
head =
new
node();
(head).data = value;
(head).height = 0;
(head).right = (head).left =
null
;
}
else
{
temp =
new
node();
temp.data = value;
temp.height = 0;
temp.right = temp.left =
null
;
left_insert(temp_head, temp);
temp_head = head;
indicator = 0;
}
return
head;
}
public
static
void
Main(String []args) {
node head =
null
, temp_head =
null
;
head = add_nodes(head, 4);
head = add_nodes(head, 12);
head = add_nodes(head, 10);
head = add_nodes(head, 5);
head = add_nodes(head, 11);
head = add_nodes(head, 8);
head = add_nodes(head, 7);
head = add_nodes(head, 6);
head = add_nodes(head, 9);
temp_head = head;
traverse(temp_head);
}
}