-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePractice.java
More file actions
168 lines (166 loc) · 4.14 KB
/
BinaryTreePractice.java
File metadata and controls
168 lines (166 loc) · 4.14 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import java.util.List;
import java.util.Queue;
import java.util.ArrayList;
import java.util.LinkedList;
public class BinaryTreePractice {
public static void main(String[] args) {
}
}
class BinarySearchTree
{
TreeNode root;
public BinarySearchTree(int x)
{
root = new TreeNode(x);
}
public TreeNode searchPre(int val)
{
TreeNode cur = root, pre = null;
while(cur!=null)
{
if(cur.val<val) {pre = cur; cur = cur.right;}
else if(cur.val>val) {pre = cur; cur = cur.left;}
else if(cur.val == val) return pre;
}
return null;
}
public TreeNode search(int val)
{
TreeNode cur = root;
while(cur!=null)
{
if(cur.val<val) cur = cur.right;
else if(cur.val>val) cur = cur.left;
else if(cur.val == val) return cur;
}
return null;
}
public void insert(int x)
{
if(root == null)
{ root = new TreeNode(x); return; }
TreeNode pre = null;
TreeNode cur = root;
while(cur!=null)
{
if(cur.val<x)
{ pre = cur; cur = cur.right;}
else if(cur.val>x)
{ pre = cur; cur = cur.left;}
else if(cur.val == x)
return;
}
if (pre.val < x) {
pre.right = new TreeNode(x);
} else {
pre.left = new TreeNode(x);
}
}
public void delete(int x)
{
if(root == null) return;
TreeNode pre = searchPre(x), target = search(x);
if(target == null)
return;
if(target.left==null||target.right==null)
{
TreeNode child = target.left != null ? target.left : target.right;
if(pre == null)
{ root = child; }
else
{
if(target == pre.left)
pre.left = child;
else
pre.right = child;
}
}
else
{
TreeNode temp = target.right;
while(temp.right!=null)
{
temp = temp.right;
}
delete(temp.val);
target.val = temp.val;
}
}
}
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x)
{
val = x;
}
}
class BinaryTree{
TreeNode root;
public BinaryTree(TreeNode x){
root = x;
}
public List<Integer> levelOrder(TreeNode root)
{
Queue<TreeNode> queue = new LinkedList<>();
List<Integer> list = new ArrayList<>();
queue.offer(root);
while(!queue.isEmpty())
{
TreeNode temp = queue.poll();
if(temp!=null)
{
list.add(temp.val);
queue.offer(temp.left);
queue.offer(temp.right);
}
}
return list;
}
public List<Integer> traversal(TreeNode root, int order)
{
List<Integer> list = new ArrayList<>();
/*preorder */
if(order == 0)
{
preOrder(root, list);
}
/*inorder */
if(order == 1)
{
inOrder(root, list);
}
/*postorder */
if(order == 2)
{
postOrder(root, list);
}
return list;
}
public void preOrder(TreeNode root, List<Integer> list)
{
if(root==null)
return;
list.add(root.val);
preOrder(root.left, list);
preOrder(root.right, list);
}
public void inOrder(TreeNode root, List<Integer> list)
{
if(root==null)
return;
preOrder(root.left, list);
list.add(root.val);
preOrder(root.right, list);
}
public void postOrder(TreeNode root, List<Integer> list)
{
if(root==null)
return;
preOrder(root.left, list);
preOrder(root.right, list);
list.add(root.val);
}
}