-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
318 additions
and
7 deletions.
There are no files selected for viewing
264 changes: 264 additions & 0 deletions
264
06-Binary-Search-Tree/02-Remove-Min-and-Max-in-BST/src/BST.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
import java.util.Stack; | ||
|
||
/** | ||
* @Auther: lss | ||
* @Date: 2019/1/30 21:40 | ||
* @Description: | ||
*/ | ||
public class BST<E extends Comparable<E>> { | ||
|
||
private class Node { | ||
public E e; | ||
public Node left, right; | ||
|
||
public Node(E e) { | ||
this.e = e; | ||
left = null; | ||
right = null; | ||
} | ||
|
||
} | ||
|
||
private Node root; | ||
private int size; | ||
|
||
public BST() { | ||
root = null; | ||
size = 0; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
// 向二分搜索树中添加新的元素 | ||
public void add(E e) { | ||
root = add(root, e); | ||
} | ||
|
||
// 向以Node为根的二分搜索树中插入元素E,递归算法 | ||
// 返回插入新节点后二分搜索树的根 | ||
private Node add(Node node, E e) { | ||
|
||
if (node == null) { | ||
size++; | ||
return new Node(e); | ||
} | ||
|
||
if (e.compareTo(node.e) < 0) | ||
node.left = add(node.left, e); | ||
else if (e.compareTo(node.e) > 0) | ||
node.right = add(node.right, e); | ||
|
||
return node; | ||
} | ||
|
||
// 看二分搜索树种是否包含元素e | ||
public boolean contains(E e) { | ||
return contains(root, e); | ||
} | ||
|
||
// 看以node为根的二分搜索树种是否包含元素e,递归算法 | ||
private boolean contains(Node node, E e) { | ||
|
||
if (node == null) | ||
return false; | ||
|
||
if (e.compareTo(node.e) == 0) | ||
return true; | ||
else if (e.compareTo(node.e) < 0) | ||
return contains(node.left, e); | ||
else // e.compareTo(node.e) > 0 | ||
return contains(node.right, e); | ||
} | ||
|
||
// 二分搜索树的前序遍历 | ||
public void preOrder() { | ||
preOrder(root); | ||
} | ||
|
||
// 前序遍历以node为根的二分搜索树,递归算法 | ||
private void preOrder(Node node) { | ||
|
||
if (node == null) | ||
return; | ||
|
||
System.out.println(node.e); | ||
preOrder(node.left); | ||
preOrder(node.right); | ||
} | ||
|
||
// 二分搜索树的非递归前序遍历 | ||
public void preOrderNR() { | ||
Stack<Node> stack = new Stack<>(); | ||
stack.push(root); | ||
while (!stack.isEmpty()) { | ||
Node cur = stack.pop(); | ||
System.out.println(cur.e); | ||
|
||
if (cur.right != null) | ||
stack.push(cur.right); | ||
if (cur.left != null) | ||
stack.push(cur.left); | ||
} | ||
} | ||
|
||
// 二分搜索树的中序遍历 | ||
public void inOrder() { | ||
inOrder(root); | ||
} | ||
|
||
// 中遍历以node 为根的二分搜索树,递归算法 | ||
private void inOrder(Node node) { | ||
|
||
if (node == null) | ||
return; | ||
|
||
inOrder(node.left); | ||
System.out.println(node.e); | ||
inOrder(node.right); | ||
} | ||
|
||
// 二分搜索树的后序遍历 | ||
public void postOrder() { | ||
postOrder(root); | ||
} | ||
|
||
// 后续遍历以node 为根的二分搜索树,递归算法 | ||
private void postOrder(Node node) { | ||
|
||
if (node == null) | ||
return; | ||
|
||
postOrder(node.left); | ||
postOrder(node.right); | ||
System.out.println(node.e); | ||
} | ||
|
||
// 二分搜索树的层序遍历 | ||
public void levelOrder() { | ||
|
||
Queue<Node> queue = new LinkedList<>(); | ||
queue.add(root); | ||
while (!queue.isEmpty()) { | ||
Node cur = queue.remove(); | ||
System.out.println(cur.e); | ||
|
||
if (cur.left != null) | ||
queue.add(cur.left); | ||
if (cur.right != null) | ||
queue.add(cur.right); | ||
} | ||
} | ||
|
||
// 寻找二分搜索树的最小元素 | ||
public E minimum() { | ||
if (size == 0) | ||
throw new IllegalArgumentException("BST is empty"); | ||
|
||
Node minNode = minimum(root); | ||
return minNode.e; | ||
} | ||
|
||
// 返回以node为根的二分搜索树的最小值所在的节点 | ||
private Node minimum(Node node) { | ||
if (node.left == null) | ||
return node; | ||
return minimum(node.left); | ||
} | ||
|
||
// 寻找二分搜索树的最大元素 | ||
public E maximum() { | ||
if (size == 0) | ||
throw new IllegalArgumentException("BST is empty"); | ||
|
||
Node maxNode = maximum(root); | ||
return maxNode.e; | ||
} | ||
|
||
// 返回以node为根的二分搜索树的最大值所在的节点 | ||
private Node maximum(Node node) { | ||
if (node.right == null) | ||
return node; | ||
return maximum(node.right); | ||
} | ||
|
||
// 从二分搜索树中删除最小值所在节点, 返回最小值 | ||
public E removeMin() { | ||
E ret = minimum(); | ||
root = removeMin(root); | ||
return ret; | ||
} | ||
|
||
// 删除掉以node为根的二分搜索树中的最小节点 | ||
// 返回删除节点后新的二分搜索树的根 | ||
private Node removeMin(Node node) { | ||
|
||
if (node.left == null) { | ||
Node rightNode = node.right; | ||
node.right = null; | ||
size--; | ||
return rightNode; | ||
} | ||
|
||
node.left = removeMin(node.left); | ||
return node; | ||
} | ||
|
||
// 从二分搜索树中删除最大值所在节点, 返回最大值 | ||
public E removeMax() { | ||
E ret = maximum(); | ||
root = removeMax(root); | ||
return ret; | ||
} | ||
|
||
// 删除掉以node为根的二分搜索树中的最大节点 | ||
// 返回删除节点后新的二分搜索树的根 | ||
private Node removeMax(Node node) { | ||
|
||
if (node.right == null) { | ||
Node leftNode = node.left; | ||
node.left = null; | ||
size--; | ||
return leftNode; | ||
} | ||
|
||
node.right = removeMax(node.right); | ||
return node; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder res = new StringBuilder(); | ||
generateBSTString(root, 0, res); | ||
return res.toString(); | ||
} | ||
|
||
// 生成以node为根节点,深度为depth的描述二叉树的字符串 | ||
private void generateBSTString(Node node, int depth, StringBuilder res) { | ||
|
||
if (node == null) { | ||
res.append(generateDepthString(depth) + "null\n"); | ||
return; | ||
} | ||
|
||
res.append(generateDepthString(depth) + node.e + "\n"); | ||
generateBSTString(node.left, depth + 1, res); | ||
generateBSTString(node.right, depth + 1, res); | ||
|
||
} | ||
|
||
private String generateDepthString(int depth) { | ||
StringBuilder res = new StringBuilder(); | ||
for (int i = 0; i < depth; i++) | ||
res.append("--"); | ||
return res.toString(); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
06-Binary-Search-Tree/02-Remove-Min-and-Max-in-BST/src/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* @Auther: lss | ||
* @Date: 2019/1/31 21:50 | ||
* @Description: | ||
*/ | ||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
BST<Integer> bst = new BST<>(); | ||
int[] nums = {3, 4, 5, 2, 8, 4}; | ||
for (int num : nums) | ||
bst.add(num); | ||
|
||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
06-Binary-Search-Tree/02-Remove-Min-and-Max-in-BST/src/Solution.java
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
06-Binary-Search-Tree/350-Intersection-of-Two-Arrays-II/src/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import java.util.ArrayList; | ||
import java.util.TreeMap; | ||
|
||
/** | ||
* @Auther: lss | ||
* @Date: 2019/2/20 14:57 | ||
* @Description: | ||
*/ | ||
public class Solution { | ||
public int[] intersect(int[] nums1, int[] nums2) { | ||
|
||
TreeMap<Integer, Integer> map = new TreeMap<>(); | ||
for (int num : nums1) { | ||
if (!map.containsKey(num)) | ||
map.put(num, 1); | ||
else | ||
map.put(num, map.get(num) + 1); | ||
} | ||
|
||
ArrayList<Integer> list = new ArrayList<>(); | ||
for (int num : nums2) { | ||
if (map.containsKey(num)) { | ||
list.add(num); | ||
map.put(num, map.get(num) - 1); | ||
if (map.get(num) == 0) | ||
map.remove(num); | ||
} | ||
} | ||
|
||
int[] res = new int[list.size()]; | ||
int i = 0; | ||
// for (int i = 0; i < list.size(); i++) | ||
// res[i] = list.get(i); | ||
for (Integer num : list) { | ||
res[i++] = num; | ||
} | ||
return res; | ||
} | ||
} |