# 树-完全二叉树

树的优点:
(1):表示某一个领域的层次结构，而链表和数组都是现行一维的，无法表示层次结构
(2):二叉排序树进行查找比链表快的多。但是这个条件并非总是成立，取决于树的结构，如果树高度平衡(任意节点的两个子树的高度差

10000此检查有着巨大的差距。

二叉排序树通过DSW算法平衡成完全二叉树算法

package cn.com.chenlly;

public class Persion {
private int id;
private String name;
private double height;

// 构造函数
public Persion(int id, String name, double height) {
this.id = id;
this.name = name;
this.height = height;
}

// 重写equals 方法
public boolean equals(Object obj) {
if (obj == null) {
return false;
}

if (obj == this) {
return true;
}

if (!(obj instanceof Persion)) {
return false;
}

Persion persion = (Persion) obj;
return persion.id == this.id && persion.name == this.name
&& persion.height == this.height;
}

// 重写 toString方法
public String toString() {
return "[id:" + id + ",name:" + name + ",height:" + height + "]";
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getHeight() {
return height;
}

public void setHeight(double height) {
this.height = height;
}
}

package cn.com.chenlly;

public class Node {
private Persion persion;  //数据域
private Node leftChild;
private Node rightChild;

public Node(Persion p){
this.persion = p;
}

public Persion getPersion() {
return persion;
}
public void setPersion(Persion persion) {
this.persion = persion;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}

}

package cn.com.chenlly;

public class BinaryTree {

private static int[] idArray = new int[]{64,32,100,90,120,130,150,10};
private Node root;

public Node getRoot() {
return root;
}

public BinaryTree() {

}

/**
* 把一个新节点插入到二叉排序树中
* @param newNode
*/
public void insert(Node newNode){
if(root == null){
root = newNode;
} else {
Node current  = root;
Node parent;
while(true){
parent = current;
if(newNode.getPersion().getId()<current.getPersion().getId()){
//当前节点下移
current = current.getLeftChild();
if(current==null){
//到了叶子节点
parent.setLeftChild(newNode);
return ;
}
} else {
current = current.getRightChild();
if(current==null){
parent.setRightChild(newNode);
return;
}
}
}
}
}

public void creatBT(){
//构造二叉排序树
for(int id:idArray){
Persion p = new Persion(id,null,0.0);
Node node = new Node(p);
insert(node);
}
}

/**
*前序遍历
* @param node
*/
public void preOrder(Node node){
if(node!=null){
System.out.print(node.getPersion().getId()+",");
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
}

/**
* 任意一颗二叉树通过一序列右旋转(right rotations)转换成一个单链结构
* （称作vine，即每个非叶子节点只有右孩子，没有左孩子），
* 也就是对节点按从小到大排序
*/
public Node createVine(Node root){
//创建一个最小的node
Node minNode = new Node(new Persion(-100,null,0.0));
Node grandpar = minNode;
Node temp = root;
while(temp!=null){
if(temp.getLeftChild()!=null){
Node child = temp.getLeftChild();
temp.setLeftChild(child.getRightChild());
child.setRightChild(temp);
temp = child;
} else {
grandpar.setRightChild(temp);
grandpar = temp;
temp = temp.getRightChild();
}
}

root = minNode.getRightChild();
return root;
}

/**
* @param args
*/
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
//创建一课二叉排序树树(BST)
bt.creatBT();
//前序遍历
System.out.println("前序遍历结果:");
bt.preOrder(bt.getRoot());
//右旋转(right rotations)转换成一个单链结构
Node root1 = bt.createVine(bt.getRoot());
System.out.println();
System.out.println("右顺转结果：");
bt.preOrder(root1);
}

}

64,32,10,100,90,120,130,150,

10,32,64,90,100,120,130,150,

bt.creatBT();创建的二叉排序树图示

bt.createVine(bt.getRoot()); 右顺转后的二叉排序树图示

• 写评论