Skip to content

Commit

Permalink
homework part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gongxun committed Feb 27, 2017
1 parent dcc74d5 commit 374d7cd
Show file tree
Hide file tree
Showing 14 changed files with 764 additions and 543 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package binarytree;

/**
* Created by william on 2017/2/16.
*/
public class BinaryTree {
private Node root;

class Node {
private Node left;
private Node right;
private Comparable data;

public Node(Node left, Node right, Comparable data) {
this.left = left;
this.right = right;
this.data = data;
}

private void add(Comparable data) {
if (this.data.compareTo(data) >= 0)
if (this.left == null)
this.left = new Node(null, null, data);
else
left.add(data);
else if (this.data.compareTo(data) < 0)
if (this.right == null)
this.right = new Node(null, null, data);
else
this.right.add(data);
}

public Comparable getData() {
return this.data;
}

public Node getLeft() {
return this.left;
}

public Node getRight() {
return this.right;
}
}

public void add(Comparable data) {
if (this.root == null)
root = new Node(null, null, data);
else this.root.add(data);
}

public void printByType(SearchType<Node> type) {
type.printByType(this.root);
}
}
package binarytree;

/**
* Created by william on 2017/2/16.
*/
public class BinaryTree {
private Node root;

class Node {
private Node left;
private Node right;
private Comparable data;

public Node(Node left, Node right, Comparable data) {
this.left = left;
this.right = right;
this.data = data;
}

private void add(Comparable data) {
if (this.data.compareTo(data) >= 0)
if (this.left == null)
this.left = new Node(null, null, data);
else
left.add(data);
else if (this.data.compareTo(data) < 0)
if (this.right == null)
this.right = new Node(null, null, data);
else
this.right.add(data);
}

public Comparable getData() {
return this.data;
}

public Node getLeft() {
return this.left;
}

public Node getRight() {
return this.right;
}
}

public void add(Comparable data) {
if (this.root == null)
root = new Node(null, null, data);
else this.root.add(data);
}

public void printByType(SearchType<Node> type) {
type.printByType(this.root);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class DLRSearchType implements SearchType<BinaryTree.Node> {

@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
System.out.print(root.getData()+" ");
printByType(root.getLeft());
printByType(root.getRight());
}
}
}
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class DLRSearchType implements SearchType<BinaryTree.Node> {

@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
System.out.print(root.getData()+" ");
printByType(root.getLeft());
printByType(root.getRight());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class LDRSearchType implements SearchType<BinaryTree.Node> {
@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
printByType(root.getLeft());
System.out.print(root.getData() + " ");
printByType(root.getRight());
}
}
}
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class LDRSearchType implements SearchType<BinaryTree.Node> {
@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
printByType(root.getLeft());
System.out.print(root.getData() + " ");
printByType(root.getRight());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package binarytree;

import java.util.LinkedList;

/**
* Created by william on 2017/2/18.
*/
public class LFSearchType implements SearchType<BinaryTree.Node> {
private LinkedList<BinaryTree.Node> queue = new LinkedList<>();

@Override
public void printByType(BinaryTree.Node root) {
if (root == null)
return;
queue.offer(root);
while (!queue.isEmpty()) {
BinaryTree.Node curNode = queue.poll();
System.out.print(curNode.getData() + " ");
if (curNode.getLeft() != null)
queue.offer(curNode.getLeft());
if (curNode.getRight() != null)
queue.offer(curNode.getRight());
}

}

}
package binarytree;

import java.util.LinkedList;

/**
* Created by william on 2017/2/18.
*/
public class LFSearchType implements SearchType<BinaryTree.Node> {
private LinkedList<BinaryTree.Node> queue = new LinkedList<BinaryTree.Node>();

@Override
public void printByType(BinaryTree.Node root) {
if (root == null)
return;
queue.offer(root);
while (!queue.isEmpty()) {
BinaryTree.Node curNode = queue.poll();
System.out.print(curNode.getData() + " ");
if (curNode.getLeft() != null)
queue.offer(curNode.getLeft());
if (curNode.getRight() != null)
queue.offer(curNode.getRight());
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class LRDSearchType implements SearchType<BinaryTree.Node> {
@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
printByType(root.getLeft());
printByType(root.getRight());
System.out.print(root.getData() + " ");
}
}
}
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public class LRDSearchType implements SearchType<BinaryTree.Node> {
@Override
public void printByType(BinaryTree.Node root) {
if (root != null) {
printByType(root.getLeft());
printByType(root.getRight());
System.out.print(root.getData() + " ");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public interface SearchType<T> {

void printByType(T root);
}
package binarytree;

/**
* Created by william on 2017/2/18.
*/
public interface SearchType<T> {

void printByType(T root);
}
Loading

0 comments on commit 374d7cd

Please sign in to comment.