-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
50 lines (48 loc) · 893 Bytes
/
Copy pathBinaryTree.java
File metadata and controls
50 lines (48 loc) · 893 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
import java.util.*;
public class BinaryTree
{
private TreeNode root;
private class TreeNode
{
private TreeNode left;
private TreeNode right;
private int data;
public TreeNode(int data)
{
this.data=data;
}
}
public void CreateBinaryTree()
{
TreeNode first=new TreeNode(1);
TreeNode second=new TreeNode(2);
TreeNode third=new TreeNode(3);
TreeNode fourth=new TreeNode(4);
TreeNode fifth=new TreeNode(5);
TreeNode sixth=new TreeNode(6);
//insertion of the data to the nodes
root=first;
first.left=second;
first.right=third;
second.left=fourth;
second.right=fifth;
third.left=sixth;
}
//preorder traversal of the binarry tree:
public void preOrder(TreeNode root)
{
if (root==null)
{
return;
}
System.out.println(root.data+" ");
preOrder(root.left);
preOrder(root.right);
}
public static void main(String[] args)
{
BinaryTree bt =new BinaryTree();
bt.CreateBinaryTree();
bt.preOrder(bt.root);
}
}