forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicBinaryTree.java
More file actions
139 lines (120 loc) · 3.23 KB
/
BasicBinaryTree.java
File metadata and controls
139 lines (120 loc) · 3.23 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.examplehub.datastructures.binarytree;
import java.util.*;
public class BasicBinaryTree<E> {
/** The root node of binary tree. */
Node<E> root;
private final StringJoiner inOrderPath;
private final StringJoiner preOrderPath;
private final StringJoiner postOrderPath;
private final StringJoiner levelOrderPath;
/**
* Constructor binary tree from root node.
*
* @param root the root node of binary tree.
*/
public BasicBinaryTree(Node<E> root) {
this.root = root;
inOrderPath = new StringJoiner("->");
preOrderPath = new StringJoiner("->");
postOrderPath = new StringJoiner("->");
levelOrderPath = new StringJoiner("->");
}
public void inorder(Node<E> root) {
if (root != null) {
inorder(root.left);
inOrderPath.add(root.value.toString());
inorder(root.right);
}
}
public String getInorder() {
if (inOrderPath.length() == 0) {
inorder(root);
}
return inOrderPath.toString();
}
public void preOrder(Node<E> root) {
if (root != null) {
preOrderPath.add(root.value.toString());
preOrder(root.left);
preOrder(root.right);
}
}
public String getPreOrder() {
if (preOrderPath.length() == 0) {
preOrder(root);
}
return preOrderPath.toString();
}
public void postOrder(Node<E> root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
postOrderPath.add(root.value.toString());
}
}
public String getPostOrder() {
if (postOrderPath.length() == 0) {
postOrder(root);
}
return postOrderPath.toString();
}
public void levelOrder(Node<E> root) {
Queue<Node<E>> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
Node<E> tempNode = queue.poll();
levelOrderPath.add(tempNode.value.toString());
if (tempNode.left != null) {
queue.add(tempNode.left);
}
if (tempNode.right != null) {
queue.add(tempNode.right);
}
}
}
public String getLevelOrder() {
if (levelOrderPath.length() == 0) {
levelOrder(root);
}
return levelOrderPath.toString();
}
@Override
public String toString() {
return getInorder();
}
/**
* Return a basic binary tree. See images/basic_binary_tree.png .
*
* @return a basic binary tree like images/basic_binary_tree.png.
*/
public static BasicBinaryTree<Integer> getBasicBinaryTree() {
Node<Integer> root = new Node<>(1);
root.left = new Node<>(2);
root.right = new Node<>(3);
root.left.left = new Node<>(4);
root.left.right = new Node<>(5);
return new BasicBinaryTree<>(root);
}
/**
* Return max depth of binary tree.
*
* @param root the root node of binary tree.
* @return max depth of binary tree.
*/
public int maxDepth(Node<E> root) {
if (root == null) {
return 0;
} else {
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
/**
* Return the count number of nodes of binary tree.
*
* @param root the root node of binary tree.
* @return the count number of nodes of binary tree.
*/
public int getNodeCount(Node<E> root) {
return root == null ? 0 : 1 + getNodeCount(root.left) + getNodeCount(root.right);
}
}