构建一个二叉树,二叉树的要求:
1)二叉树的父节点小于子节点的两个值;
2)二叉树的左子树比右子树小;
3)二叉树的高度尽量小。
import java.util.ArrayList;
public class BinaryTree {
// 整个程序在没有重复之的前提下
public static void main(String[] args) {
Tree t = new Tree();
int[] tree = {432,234,345,56,73,1,344,87,232,98};
for (int i : tree) {
t.add(i);
}
// 输出二叉树的高度
// 输出二叉树结构
Node root = t.getRoot();
System.out.println("The root node is "+root.getVal());
System.out.println("The tree's height is "+t.getLevel());
ArrayList<Node> list = new ArrayList<Node>();
list.add(root);
System.out.println("The tree's construct is ");
while (list.size() > 0) {
ArrayList<Node> temp = new ArrayList<Node>();
for (Node n : list) {
System.out.print("\t" + n.getVal());
if (!(n.getLeft() == null)) {
temp.add(n.getLeft());
if (!(n.getRight() == null)) {
temp.add(n.getRight());
}
}
}
System.out.println();
list = temp;
}
System.out.println("The all node:");
for(Node n:t.getList()){
System.out.println(n);
}
// 输出二叉树的所有叶子节点
System.out.println("The leaf node:");
for (Node n : t.getLeaf()) {
System.out.println(n+"\t");
}
}
}
class Tree {
private ArrayList<Node> list = new ArrayList<Node>();
private Node last;
public ArrayList<Node> getList(){
return list;
}
public Node getRoot() {
if (list.size() > 0) {
Node n = list.get(0);
while (!(n.getParent() == null)) {
n = n.getParent();
}
return n;
}
return null;
}
// 得到tree的高度
public int getLevel() {
Node n = last;
int level = 1;
if (!(last.getLeft() == null)) {
level += 1;
}
while (!(n.getParent() == null)) {
level++;
n = n.getParent();
}
return level;
}
public void add(int value) {
Node n = new Node(value);
list.add(n);
if (list.size() <= 1) {
last = n;
} else {
if (last.getLeft() == null) {
last.setLeft(n);
n.setParent(last);
check(n);
} else {
last.setRight(n);
n.setParent(last);
check(n);
int nint = find(last);
last = list.get(nint + 1);
}
}
System.out.println("Add "+n+" to list. The last is "+last);
}
private void check(Node node){
System.out.println("\tCheck : "+node);
boolean flag = true;
while(flag){
if(node.getParent() == null){
flag = false;
}else{
Node parent = node.getParent();
Node nl = parent.getLeft();
Node nr = parent.getRight();
if(node.getVal()<parent.getVal()){
change(node, parent);
nl = node.getLeft();
nr = node.getRight();
// node = parent;
}else{
flag = false;
}
if(!(nr == null)){
if(nl.getVal()>nr.getVal()){
change(nr, nl);
}
}
}
}
}
// 查找到节点在list中的索引指
private int find(Node node) {
int res = -1;
for (int i = list.size()-1; i >= 0; i--) {
if (list.get(i).getVal() == node.getVal()) { // 不存在重复值的情况下
res = i;
}
}
return res;
}
private void change(Node n1, Node n2){
if(n1.getParent().equals(n2)){
changeCP(n1, n2);
}else if(n2.getParent().equals(n1)){
changeCP(n2, n1);
}else{
Node n1l = n1.getLeft();
Node n1r = n1.getRight();
Node n1p = n1.getParent();
Node n2l = n2.getLeft();
Node n2r = n2.getRight();
Node n2p = n2.getParent();
if(!(n1l == null) && !n1l.equals(n2)){
n2.setLeft(n1l);
n1l.setParent(n2);
}
if(!(n1r == null) && !n1r.equals(n2)){
n2.setRight(n1r);
n1r.setParent(n2);
}
if(!(n1p == null) && !n1p.equals(n2)){
n2.setParent(n1p);
if(n1p.getLeft().getVal()==n1.getVal()){
n1p.setLeft(n2);
}else{
n1p.setRight(n2);
}
}
if(!(n2l == null) && !n2l.equals(n1)){
n1.setLeft(n2l);
n2l.setParent(n1);
}
if(!(n2r == null) && !n2r.equals(n1)){
n1.setRight(n2r);
n2r.setParent(n1);
}
if(!(n2p == null) && !n2p.equals(n1)){
n1.setParent(n2p);
if(n2p.getLeft().getVal()==n2.getVal()){
n2p.setLeft(n1);
}else{
n2p.setRight(n1);
}
}
}
changeLocal(n1, n2);
if(last.equals(n1)){
last = n2;
}else if(last.equals(n2)){
last = n1;
}
}
/**
* @param child
* @param parent
*/
private void changeCP(Node child, Node parent){
Node pl = parent.getLeft();
Node pr = parent.getRight();
Node pp = parent.getParent();
parent.setLeft(child.getLeft());
if(!(child.getLeft() == null)){
child.getLeft().setParent(parent);
}
parent.setRight(child.getRight());
if(!(child.getRight() == null)){
child.getRight().setParent(parent);
}
parent.setParent(child);
if(pl.equals(child)){
child.setLeft(parent);
child.setRight(pr);
if(!(pr == null))
pr.setParent(child);
}else{
child.setRight(parent);
child.setLeft(pl);
if(!(pl == null))
pl.setParent(child);
}
child.setParent(pp);
if(!(pp == null)){
if(pp.getLeft().getVal()==parent.getVal()){
pp.setLeft(child);
}else{
pp.setRight(child);
}
}
}
// 互换两个节点在list中的位置
private void changeLocal(Node n1, Node n2) {
int i1 = find(n1);
int i2 = find(n2);
list.set(i1, n2);
list.set(i2, n1);
}
public ArrayList<Node> getLeaf() {
ArrayList<Node> res = new ArrayList<Node>();
for (Node n : list) {
if (n.getLeft() == null && n.getRight() == null) {
res.add(n);
}
}
return res;
}
}
class Node {
private int val;
private Node left = null;
private Node right = null;
private Node parent = null;
public Node getOther(Node n) {
if (right.getVal() == n.getVal()) {
return left;
}
return right;
}
public Node(int val) {
this.val = val;
}
public void setVal(int val) {
this.val = val;
}
public int getVal() {
return val;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getLeft() {
return left;
}
public void setRight(Node right) {
this.right = right;
}
public Node getRight() {
return right;
}
public void setParent(Node parent) {
this.parent = parent;
}
public Node getParent() {
return parent;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + val;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (val != other.val)
return false;
return true;
}
@Override
public String toString() {
return "Node [val=" + val + ", left=" + (left==null?"null":left.getVal())
+ ", right=" + (right==null?"null":right.getVal())
+ ", parent=" + (parent==null?"null":parent.getVal()) + "]";
}
}