-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
53 lines (51 loc) · 1.03 KB
/
BinaryTree.java
File metadata and controls
53 lines (51 loc) · 1.03 KB
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
class Node {
/*Created node with right left and a data*/
Node right;
Node left;
int data;
Node(int data) {
this.data = data;
this.right = right;
this.left = left;
}
}
class Tree {
Node root;
public Tree(Node root) {
this.root = root;
}
void insertNode(Node node, int data) {
if(node.data > data) {
if(node.left != null)
insertNode(node.left, data);
else {
Node newNode = new Node(data);
node.left = newNode;
}
}
else {
if(node.right != null)
insertNode(node.right, data);
else {
Node newNode = new Node(data);
node.right = newNode;
}
}
}
void print(Node root) {
if(root == null)
return;
print(root.left);
System.out.println(root.data);
print(root.right);
}
}
/*Printing data in a descending manner*/
class BinaryTree{
public static void main(String[] args) {
Tree b = new Tree(new Node(50));
b.insertNode(b.root,25);
b.insertNode(b.root,75);
b.print(b.root);
}
}