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 906f85b

Browse files
committed
Tree Sum added
1 parent 8ddb4af commit 906f85b

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

‎AlgorithmCode/TreeSum.java‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**
2+
* Tree sum example
3+
*
4+
* <p>Download the code: <br>
5+
* $ git clone https://github.com/williamfiset/Algorithms
6+
*
7+
* <p>Time Complexity: O(n)
8+
*
9+
* @author William Fiset, william.alexandre.fiset@gmail.com
10+
*/
11+
12+
import java.util.*;
13+
14+
public class TreeSum {
15+
16+
public static class TreeNode {
17+
int value;
18+
List<TreeNode> children = new ArrayList<>();
19+
20+
public TreeNode(int value) {
21+
this.value = value;
22+
}
23+
24+
public int getValue() {
25+
return value;
26+
}
27+
28+
public List<TreeNode> getChildren() {
29+
return children;
30+
}
31+
32+
public void addChild(TreeNode... nodes) {
33+
for (TreeNode node : nodes) {
34+
children.add(node);
35+
}
36+
}
37+
}
38+
39+
public static int treeSum(TreeNode node) {
40+
if (node == null)
41+
return 0;
42+
int total = node.value;
43+
for (TreeNode child : node.children) {
44+
total += treeSum(child);
45+
}
46+
return total;
47+
}
48+
49+
/* Examples */
50+
51+
public static void main(String[] args) {
52+
TreeNode root = makeTree();
53+
System.out.printf("Tree sum: %d\n", treeSum(root));
54+
}
55+
56+
private static TreeNode makeTree() {
57+
TreeNode root = new TreeNode(5);
58+
59+
TreeNode node4 = new TreeNode(4);
60+
TreeNode node3 = new TreeNode(3);
61+
root.addChild(node4, node3);
62+
63+
TreeNode node1 = new TreeNode(1);
64+
TreeNode nodem6 = new TreeNode(-6);
65+
node4.addChild(node1, nodem6);
66+
67+
TreeNode node0 = new TreeNode(0);
68+
TreeNode node7 = new TreeNode(7);
69+
TreeNode nodem4 = new TreeNode(-4);
70+
node3.addChild(node0, node7, nodem4);
71+
72+
TreeNode node2 = new TreeNode(2);
73+
TreeNode node9 = new TreeNode(9);
74+
node1.addChild(node2, node9);
75+
76+
TreeNode node8 = new TreeNode(8);
77+
node7.addChild(node8);
78+
79+
return root;
80+
}
81+
}

‎README.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
3737

3838
- [Diameter of a Binary Tree](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/DiameterOfTree.java) - 트리의 지름(트리에서 가장 긴 경로의 노드 개수)
3939

40+
- [Sum of Nodes](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/TreeSum.java) - 트리에서의 노드들의 합
41+
4042
- Sum of Leaf Nodes - 잎 노드들의 합
4143

4244

@@ -60,15 +62,15 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
6062
### Sorting - 정렬
6163
- [Quick sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/QuickSort.java) - 빠른정렬, Worst case: O(n^2), Average case: O(nlogn) where n is the number of item in an array
6264

63-
- [Merge sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/MergeSort.java) - 병합정렬 O(nlogn)
65+
- [Merge sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/MergeSort.java) - 병합정렬 O(nlogn), Worst case: O(nlogn) where n is the number of item in an array
6466

65-
- [Counting sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/CountingSort.java) - 카운팅 소트, 계수정렬, O(kn) where k is upper bound
67+
- [Counting sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/CountingSort.java) - 카운팅 소트, 계수정렬, O(kn) where k is upper bound number, n is the # of items in an array
6668

67-
- [Selection sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/SelectionSort.java) - 선택정렬 O(n^2)
69+
- [Selection sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/SelectionSort.java) - 선택정렬 Worst case: O(n^2)
6870

69-
- [Bubble sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/BubbleSort.java) - 버블소트 O(n^2)
71+
- [Bubble sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/BubbleSort.java) - 버블소트 Worst case: O(n^2)
7072

71-
- [Insertion sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/InsertionSort.java) - 삽입정렬 O(n^2)
73+
- [Insertion sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/InsertionSort.java) - 삽입정렬 Worst case: O(n^2)
7274

7375
- [Heap sort](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Sorting/HeapSort.java) - 힙 정렬 Worst case: O(nlogn)
7476

@@ -81,7 +83,7 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
8183

8284
- [Topological Sort - Using DFS](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/TopologicalSortUsingDFS.java) - Using DFS, O(V+E)
8385

84-
### Dynamic Programming - 동적 프로그래밍
86+
### Dynamic Programming - 동적 프로그래밍 (잘 알려진 문제들)
8587
- [Fibonacci - Bottom up Manner](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Fibonacci.java) - n번째 피보나치 수열 구하기(반복적 방법), Iterative
8688

8789
- [Fibonacci - Top Down Manner](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/Fibonacci_Recursion.java) - n번째 피보나치 수열 구하기(재귀적 방법), Recursive + Memoization
@@ -166,9 +168,9 @@ In mathematics and computer science, an algorithm is a finite sequence of well-d
166168
- [LCA - Naive Approach](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/LCA_Naive.java) LCA Query = O(n)
167169

168170
### String Pattern Matching - 문자열 패턴 매칭
169-
- [KMP Pattern Matching Algorithm](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/KMP.java) - KMP(Knuth, Morris, Pratt) 패턴 매칭 알고리즘, O(n+m) - n is pattern matching and m is lps construction
171+
- [KMP Pattern Matching Algorithm](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/KMP.java) - KMP(Knuth, Morris, Pratt) 패턴 매칭 알고리즘, O(n+m) where n is pattern matching and m is LPS construction (LPS : Longest Proper Prefix which is also Suffix)
170172

171-
- [Boyer Moore Algorithm](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/BoyerMoore.java) - Bad Character Rule
173+
- [Boyer Moore Algorithm](https://github.com/lemidia/Algorithm-and-Data-Structure/blob/master/AlgorithmCode/BoyerMoore.java) - Using Bad Character Rule
172174

173175
- Rabin Karp Algorithm
174176

0 commit comments

Comments
(0)

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