-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenaral Trees in Java
68 lines (54 loc) · 1.55 KB
/
Genaral Trees in Java
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
60
61
62
63
64
65
66
67
68
import java.util.ArrayList;
import java.util.List;
// Node class
class TreeNode {
String data;
List<TreeNode> children;
// Constructor
public TreeNode(String data) {
this.data = data;
this.children = new ArrayList<>();
}
// Add a child to this node
public void addChild(TreeNode child) {
children.add(child);
}
}
// GeneralTree class
public class GeneralTree {
TreeNode root;
// Constructor
public GeneralTree(String rootData) {
this.root = new TreeNode(rootData);
}
// Print the tree
public void printTree() {
printTree(root, "");
}
// Recursive method to print the tree
private void printTree(TreeNode node, String indent) {
if (node != null) {
System.out.println(indent + node.data);
for (TreeNode child : node.children) {
printTree(child, indent + " ");
}
}
}
public static void main(String[] args) {
GeneralTree tree = new GeneralTree("CEO");
TreeNode cto = new TreeNode("CTO");
TreeNode coo = new TreeNode("COO");
TreeNode cfo = new TreeNode("CFO");
TreeNode dev1 = new TreeNode("Dev1");
TreeNode dev2 = new TreeNode("Dev2");
TreeNode acc1 = new TreeNode("Acc1");
tree.root.addChild(cto);
tree.root.addChild(coo);
tree.root.addChild(cfo);
cto.addChild(dev1);
cto.addChild(dev2);
cfo.addChild(acc1);
System.out.println("Tree Structure:");
tree.printTree();
}
}