Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6a58c3

Browse files
Added Trees Topic
1 parent c339d63 commit d6a58c3

File tree

3 files changed

+212
-0
lines changed

3 files changed

+212
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
5+
public class Tree {
6+
// TREE NODE
7+
public static class TreeNode {
8+
9+
public Object data;
10+
public ArrayList<TreeNode> children;
11+
12+
public TreeNode(Object data) {
13+
this.data = data;
14+
this.children = new ArrayList<TreeNode>();
15+
}
16+
17+
public void addChild(TreeNode child) {
18+
this.children.add(child);
19+
}
20+
21+
public void addChild(Object childData) {
22+
TreeNode child = new TreeNode(childData);
23+
this.children.add(child);
24+
}
25+
26+
public void removeChild(TreeNode childToRemove) {
27+
if (this.children.isEmpty()) {
28+
return;
29+
} else if (this.children.contains(childToRemove)) {
30+
this.children.remove(childToRemove);
31+
return;
32+
} else {
33+
for (TreeNode child : this.children) {
34+
child.removeChild(childToRemove);
35+
}
36+
}
37+
}
38+
39+
public void removeChild(Object data) {
40+
if (this.children.isEmpty()) {
41+
return;
42+
}
43+
for (TreeNode child : this.children) {
44+
if (child.data == data) {
45+
removeChild(child);
46+
return;
47+
}
48+
}
49+
for (TreeNode child : this.children) {
50+
child.removeChild(data);
51+
}
52+
}
53+
}
54+
55+
// TREE IMPLEMENTATION
56+
public TreeNode root;
57+
58+
public Tree(TreeNode root) {
59+
this.root = root;
60+
}
61+
62+
public void print() {
63+
print(this.root, 0);
64+
}
65+
66+
public void print(TreeNode current, int level) {
67+
String levelMarks = "";
68+
for (int i = 0; i < level; i++) {
69+
levelMarks += "-- ";
70+
}
71+
System.out.println(levelMarks + current.data);
72+
for (TreeNode child : current.children) {
73+
print(child, level + 1);
74+
}
75+
}
76+
77+
// Depth First Traversal
78+
public void depthFirstTraversal(TreeNode current) {
79+
System.out.print(current.data + " ");
80+
for (TreeNode child : current.children) {
81+
depthFirstTraversal(child);
82+
}
83+
}
84+
85+
// Breadth First Traversal
86+
public void breadthFirstTraversal(){
87+
TreeNode current = this.root;
88+
Queue <TreeNode> queue = new LinkedList<>();
89+
queue.add(current);
90+
while (!queue.isEmpty()){
91+
current = queue.poll();
92+
System.out.print(current.data + " ");
93+
queue.addAll(current.children);
94+
}
95+
96+
}
97+
98+
public static void main(String[] args) {
99+
TreeNode treeRoot = new TreeNode("1");
100+
TreeNode child1 = new TreeNode("2");
101+
TreeNode child2 = new TreeNode("3");
102+
TreeNode grandchild1 = new TreeNode("4");
103+
TreeNode grandchild2 = new TreeNode("5");
104+
treeRoot.addChild(child1);
105+
treeRoot.addChild(child2);
106+
child1.addChild(grandchild1);
107+
child2.addChild(grandchild2);
108+
109+
Tree tree = new Tree(treeRoot);
110+
tree.print();
111+
112+
System.out.println(" ");
113+
114+
System.out.println("Breadth First Traversal");
115+
tree.breadthFirstTraversal();
116+
117+
System.out.println(" ");
118+
System.out.println(" ");
119+
120+
System.out.println("Depth First Traversal");
121+
tree.depthFirstTraversal(treeRoot);
122+
123+
}
124+
125+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.ArrayList;
2+
3+
public class TreeNode {
4+
5+
public Object data;
6+
public ArrayList<TreeNode> children;
7+
8+
public TreeNode(Object data) {
9+
this.data = data;
10+
this.children = new ArrayList<TreeNode>();
11+
}
12+
13+
public void addChild(TreeNode child) {
14+
this.children.add(child);
15+
}
16+
17+
public void addChild(Object childData) {
18+
TreeNode child = new TreeNode(childData);
19+
this.children.add(child);
20+
}
21+
22+
public void removeChild(TreeNode childToRemove) {
23+
if (this.children.isEmpty()) {
24+
return;
25+
} else if (this.children.contains(childToRemove)) {
26+
this.children.remove(childToRemove);
27+
return;
28+
} else {
29+
for (TreeNode child : this.children) {
30+
child.removeChild(childToRemove);
31+
}
32+
}
33+
}
34+
35+
public void removeChild(Object data) {
36+
if (this.children.isEmpty()) {
37+
return;
38+
}
39+
for (TreeNode child : this.children) {
40+
if (child.data == data) {
41+
removeChild(child);
42+
return;
43+
}
44+
}
45+
for (TreeNode child : this.children) {
46+
child.removeChild(data);
47+
}
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Trees
2+
Trees are an essential data structure for storing hierarchical data with a directed flow.
3+
4+
Similar to linked lists and graphs, trees are composed of nodes which hold data. The diagram represents nodes as rectangles and data as text.
5+
6+
Nodes also store references to zero or more other tree nodes. Data moves down from node to node. We depict those references as lines drawn between rectangles.
7+
8+
Trees are often displayed with a single node at the top and connected nodes branching downwards.
9+
10+
## Varietals
11+
12+
Trees come in various shapes and sizes depending on the dataset modeled.
13+
14+
Some are wide, with parent nodes referencing many child nodes.
15+
16+
Some are deep, with many parent-child relationships.
17+
18+
Each time we move from a parent to a child, we’re moving down a level.
19+
20+
Depending on the orientation we refer to this as the depth (counting levels down from the root node) or height (counting levels up from a leaf node).
21+
22+
## Structure
23+
24+
A tree is composed of tree nodes. There is a root tree node, and children tree nodes which may or may not be parent nodes themselves.
25+
26+
A tree node is a very simple data structure that contains:
27+
28+
* Data
29+
* A list of children, where each child is itself a tree node
30+
* Pointer to a parent tree node (Optional)
31+
32+
## Depth First Traversal
33+
Depth-first-search (DFS) is a technique that visits the first child in the children list and that node’s children recursively before visiting all the first child’s siblings and then their children recursively.
34+
35+
## Breadth First Traversal
36+
Breadth-first-search (BFS) is a technique in a tree that visits all children of a node first before visiting any further levels.
37+
38+
Both are implemented in the code.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /