Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.wdn.coding2017.basic;

import java.util.ArrayList;
import java.util.List;

/**
* Created by wdn on 2017/5/30 0030.
*/
public class OutOfMemory {
private List<Object> list = new ArrayList<>();
private void stackFunction(){
stackFunction();
}
private void outOfMemory(){
while (true) {
list.add(new int[1024 * 1000]);
}
}
private void outOfMemoryPermGenSpace(){
// java8 设置-server -XX:MetaspaceSize=1M -XX:MaxMetaspaceSize=2m
// java8 之前设置-server -XX:PermSize=64M -XX:MaxPermSize=128M

}
public static void main(String[] args) {
OutOfMemory out = new OutOfMemory();
// StackOverflowError
//out.stackFunction();
// OutOfMemoryError: Java heap space
//out.outOfMemory();
// OutOfMemory :PermGen space
//out.outOfMemoryPermGenSpace();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.github.wdn.coding2017.basic.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
* Created by wangxin on 2017/5/26.
*/
public class BinarySearchTree<T extends Comparable> {
BinaryTreeNode<T> root;
public BinarySearchTree(BinaryTreeNode<T> root){
this.root = root;
}
public BinaryTreeNode<T> getRoot(){
return root;
}
public T findMin(){
if (root == null) {
throw new RuntimeException("tree is null");
}
BinaryTreeNode<T> local = root;
while (local.getLeft()!=null){
local = local.getLeft();
}
return local.data;
}
public T findMax(){
if (root == null) {
throw new RuntimeException("tree is null");
}
BinaryTreeNode<T> local = root;
while (local.getRight()!=null){
local = local.getRight();
}
return local.data;
}
public int height() {
if(root==null){
return 0;
}
int leftDeep = findChild(root.getLeft(),2);
int rightDeep = findChild(root.getRight(),2);
return Math.max(leftDeep,rightDeep);
}
private int findChild(BinaryTreeNode<T> node,int deepIndex){
if(node.getLeft()!=null){
findChild(node.getLeft(),deepIndex++);
}
if(node.getRight()!=null){
findChild(node.getRight(),deepIndex++);
}
return deepIndex;
}
public int size() {
if(root==null){
return 0;
}
int count = sumChild(root)+1;
return count;
}
private int sumChild(BinaryTreeNode<T> node){
int count = 0;
if(node.getLeft()!=null){
count++;
count+=sumChild(node.getLeft());
}
if(node.getRight()!=null){
count++;
count+=sumChild(node.getRight());
}
return count;
}
public void remove(T e){

}
public List<T> levelVisit(){
List<T> values = new ArrayList<>();
if(root == null){
return values;
}

Queue<BinaryTreeNode<T>> queue = new LinkedList<>();//层序遍历时保存结点的队列
queue.offer(root);//初始化
while(!queue.isEmpty()){
BinaryTreeNode<T> node = queue.poll();
//System.out.print(node.data + " ");//访问节点
values.add(node.data);
if(node.left != null)
queue.offer(node.left);
if(node.right != null)
queue.offer(node.right);
}
return values;
}
public boolean isValid(){
if (root == null) {
return true;
}
return dfs(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean dfs(BinaryTreeNode root, Comparable low, Comparable up) {
if (root == null) {
return true;
}
if (root.data.compareTo(up) >= 0 || root.data.compareTo(low) <= 0) {
return false;
}
return dfs(root.left, low, root.data) && dfs(root.right, root.data, up);
}
public T getLowestCommonAncestor(BinaryTreeNode<T> nodeData, T node1, T node2){
if(nodeData == null){
return null;
}
if (nodeData.data == node1 || nodeData.data == node2) {
return nodeData.data;
}

// Divide
T left = getLowestCommonAncestor((BinaryTreeNode<T>)nodeData.left, node1, node2);
T right = getLowestCommonAncestor((BinaryTreeNode<T>)nodeData.right, node1, node2);

// Conquer
if (left != null && right != null) {
return nodeData.data;
}
if (left != null) {
return left;
}
if (right != null) {
return right;
}
return null;
}
public List<T> getNodesBetween(T n1, T n2){
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.github.wdn.coding2017.basic.tree;

import java.util.List;

/**
* Created by wangxin on 2017/5/26.
*/

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;



public class BinarySearchTreeTest {

BinarySearchTree<Integer> tree = null;

@Before
public void setUp() throws Exception {
BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6);
root.left = new BinaryTreeNode<Integer>(2);
root.right = new BinaryTreeNode<Integer>(8);
root.left.left = new BinaryTreeNode<Integer>(1);
root.left.right = new BinaryTreeNode<Integer>(4);
root.left.right.left = new BinaryTreeNode<Integer>(3);
root.left.right.right = new BinaryTreeNode<Integer>(5);
tree = new BinarySearchTree<Integer>(root);
}

@After
public void tearDown() throws Exception {
tree = null;
}

@Test
public void testFindMin() {
Assert.assertEquals(1, tree.findMin().intValue());

}

@Test
public void testFindMax() {
Assert.assertEquals(8, tree.findMax().intValue());
}

@Test
public void testHeight() {
Assert.assertEquals(4, tree.height());
}

@Test
public void testSize() {
Assert.assertEquals(7, tree.size());
}

@Test
public void testRemoveLeaf() {
tree.remove(3);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(4, root.left.right.data);

}
@Test
public void testRemoveMiddleNode1() {
tree.remove(4);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(5, root.left.right.data);
Assert.assertEquals(3, root.left.right.left.data);
}
@Test
public void testRemoveMiddleNode2() {
tree.remove(2);
BinaryTreeNode<Integer> root= tree.getRoot();
Assert.assertEquals(3, root.left.data);
Assert.assertEquals(4, root.left.right.data);
}

@Test
public void testLevelVisit() {
List<Integer> values = tree.levelVisit();
Assert.assertEquals("[6, 2, 8, 1, 4, 3, 5]", values.toString());

}
@Test
public void testLCA(){
Assert.assertEquals(2,tree.getLowestCommonAncestor(tree.getRoot(),1, 5).intValue());
Assert.assertEquals(2,tree.getLowestCommonAncestor(tree.getRoot(),1, 4).intValue());
Assert.assertEquals(6,tree.getLowestCommonAncestor(tree.getRoot(),3, 8).intValue());
}
@Test
public void testIsValid() {

Assert.assertTrue(tree.isValid());

BinaryTreeNode<Integer> root = new BinaryTreeNode<Integer>(6);
root.left = new BinaryTreeNode<Integer>(2);
root.right = new BinaryTreeNode<Integer>(8);
root.left.left = new BinaryTreeNode<Integer>(4);
root.left.right = new BinaryTreeNode<Integer>(1);
root.left.right.left = new BinaryTreeNode<Integer>(3);
tree = new BinarySearchTree<Integer>(root);

Assert.assertFalse(tree.isValid());
}
@Test
public void testGetNodesBetween(){
List<Integer> numbers = this.tree.getNodesBetween(3, 8);
System.out.println(numbers.toString());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.github.wdn.coding2017.basic.tree;

/**
* Created by wangxin on 2017/5/20.
*/
public class BinaryTreeNode<T extends Comparable> {

public T data;
public BinaryTreeNode left;
public BinaryTreeNode right;
public BinaryTreeNode() {
}
public BinaryTreeNode(T data) {
this.data = data;
}

public BinaryTreeNode insert(T o){
BinaryTreeNode result;
if(o.compareTo(data)<0){
if(this.left==null){
BinaryTreeNode newNode = new BinaryTreeNode();
newNode.setData(o);
setLeft(newNode);
result = newNode;
}else{
result = this.left.insert(o);
}
}else{
if(this.right==null){
BinaryTreeNode newNode = new BinaryTreeNode();
newNode.setData(o);
setRight(newNode);
result = newNode;
}else{
result = this.right.insert(o);
}
}
return result;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}

}
Loading