Skip to content

Commit

Permalink
二叉树
Browse files Browse the repository at this point in the history
  • Loading branch information
Pan committed Feb 26, 2017
1 parent 551df02 commit cf46536
Showing 1 changed file with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,74 @@
/**
* Created by QiPan on 2017/2/23.
*/
public class BinaryTreeNode {
public class BinaryTreeNode<Key extends Comparable<Key>, Value> {

private Object data;
private BinaryTreeNode left;
private BinaryTreeNode right;
private Node root;

public Object getData() {
return data;
private class Node {
private Key key;
private Value value;
private Node left, right; //指向子树的链接
private int N; // 以该节点为根的子树节点总数
public Node(Key key, Value value, int N) {
this.key = key;
this.value = value;
this.N = N;
}
}
public void setData(Object data) {
this.data = data;

public int size() {
return size(root);
}
public BinaryTreeNode getLeft() {
return left;

private int size(Node x) {
if (x == null) return 0;
else return x.N;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;

public Value get(Key key){
return get(root, key);
}
public BinaryTreeNode getRight() {
return right;

private Value get(Node x, Key key) {
// 如果根节点也是Null 那么返回null
if (x == null){
return null;
}
// 拿需要查询的key 与 根节点的 key 比较
int cmp = key.compareTo(x.key);
if (cmp < 0){ // 如果小于0 那么去左子树上查询,并且递归调用
return get(x.left, key);
}else if (cmp > 0){// 如果大于0 那么去右子树上查询,并且递归调用
return get(x.right, key);
}else {
return x.value;
}
}
public void setRight(BinaryTreeNode right) {
this.right = right;

public void push(Key key, Value value){
// 查询key, 找到则更新它的值,否则就创建
root = put(root, key, value);
}

public BinaryTreeNode insert(Object o){
return null;
private Node put(Node x, Key key, Value value) {
// 如果key 存在于以 x 为根节点的子树中 则更新它的值
// 否则将以key 和 value 为键值对的新节点插入到该子树中

if (x == null){
return new Node(key, value, 1);
}
int cmp = key.compareTo(x.key);
if (cmp < 0 ){
x.left = put(x.left, key, value);
}else if (cmp > 0){
x.right = put(x.right, key, value);
}else { // 存在更新值
x.value = value;
}
// 重新统计节点总数
x.N = size(x.left) + size(x.right) + 1;
return x;
}

}

0 comments on commit cf46536

Please sign in to comment.