Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create BT_in_java.java #1411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions java codes/BT_in_java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Class containing left and right child of current
node and key value*/
class Node
{
int key;
Node left, right;

public Node(int item)
{
key = item;
left = right = null;
}
}

// A Java program to introduce Binary Tree
class BinaryTree
{
// Root of Binary Tree
Node root;

// Constructors
BinaryTree(int key)
{
root = new Node(key);
}

BinaryTree()
{
root = null;
}

public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();

/*create root*/
tree.root = new Node(1);

/* following is the tree after above statement

1
/ \
null null */

tree.root.left = new Node(2);
tree.root.right = new Node(3);

/* 2 and 3 become left and right children of 1
1
/ \
2 3
/ \ / \
null null null null */


tree.root.left.left = new Node(4);
/* 4 becomes left child of 2
1
/ \
2 3
/ \ / \
4 null null null
/ \
null null
*/
}
}