-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTree.java
More file actions
59 lines (49 loc) · 1005 Bytes
/
Copy pathTree.java
File metadata and controls
59 lines (49 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package trees.binaryTree;
//class containing left & right child of current node & 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
public class Tree {
// Root node of binary tree
Node root;
// Constructors
Tree(int key) {
root = new Node(key);
}
Tree() {
root = null;
}
public static void main(String[] args) {
Tree t = new Tree();
// create root
t.root = new Node(1);
/*
* following is the tree after above statement
* 1
* / \
* null null
*/
t.root.left = new Node(2);
t.root.right = new Node(3);
/*
* 2 & 3 become left & right children of 1
* 1
* / \
* / \
* / \
* 2 3
* / \ / \
* null null null null
*/
t.root.left.left = new Node(4);
t.root.right.right = new Node(5);
t.root.right.left = new Node(6);
t.root.left.right = new Node(7);
}
}