public class twoTree {
private static Node root;
public class Node{
private int data;
private Node left;
private Node right;
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
public void insert(int data){
root = creatNode(root,data);
}
public Node creatNode(Node node, int data){
if(node == null){
node = new Node();
node.setData(data);
}else{
if(node.getData()>data){
node.setLeft(creatNode(node.getLeft(),data));
}else{
node.setRight(creatNode(node.getRight(),data));
}
}
return node;
}
/** 只实现有一个节点的删除 */
public boolean delete(int key){
Node current = root;
Node parent = null;
boolean isLeftChild = false;
while (current.getData() != key){
parent = current;
if (key < current.getData()){
current = current.getLeft();
isLeftChild = true;
}else{
current = current.getRight();
isLeftChild = false;
}
}
/** 无子节点 */
if (current.getLeft() == null && current.getRight() == null){
if (current == root){
root = null;
}else if (isLeftChild){
Node del = null;
parent.setLeft(del);
}else{
Node del = null;
parent.setRight(del);
}
}
/**
* 三种情况
* 1仅有右节点
* 2仅有左节点
* 3左右都有
* 符号 && 不是 & 呵呵
*/
/** 仅有右节点 */
else if ((current.getLeft() == null && current.getRight() != null)){
if (current == root){
root = current.getRight();
}else if (isLeftChild){
parent.setLeft(current.getRight());
}else{
parent.setRight(current.getRight());
}
/** 仅有左节点 */
}else if ((current.getLeft() != null && current.getRight() == null)){
if (current == root){
root = null;
}else if (isLeftChild){
parent.setLeft(current.getLeft());
}else{
parent.setRight(current.getLeft());
}
}
return true;
}
public Node find(int key){
Node node = root;
while (node != null){
if (node.getData() == key){
break;
}else if (node.getData() > key){
System.out.println("left"+node.getData());
node = node.getLeft();
}else{
System.out.println("right"+node.getData());
node = node.getRight();
}
}
return node;
}
public void readDatas(int data[]){
for(int i:data){
insert(i);
}
}
public void printTree(){
leftPrinTree(root);
System.out.println();
rightPrinTree(root);
System.out.println();
PrinTree(root);
System.out.println();
delete(78);
System.out.println(">>>>>>>>>>>>>>>>");
leftPrinTree(root);
System.out.println();
rightPrinTree(root);
System.out.println();
PrinTree(root);
//System.out.println(find(45).data+"@@@@@@@@@");
}
/**
* 前序遍历,也叫先根遍历,遍历的顺序是,根,左子树,右子树
* @param node
*/
public void leftPrinTree(Node node){
if(node == null) return ;
System.out.print(node.getData()+",");
leftPrinTree(node.getLeft());
leftPrinTree(node.getRight());
}
/**
* 中序遍历,也叫中根遍历,顺序是 左子树,根,右子树
* @param node
*/
public void rightPrinTree(Node node){
if(node == null) return ;
rightPrinTree(node.getLeft());
System.out.print(node.getData()+",");
rightPrinTree(node.getRight());
}
/**
* 后序遍历,也叫后根遍历,遍历顺序,左子树,右子树,根
* @param node
*/
public void PrinTree(Node node){
if(node == null) return ;
PrinTree(node.getLeft());
PrinTree(node.getRight());
System.out.print(node.getData()+",");
}
public static void main(String []args){
int data [] ={7,12,1,4,50,78,33,55,56,45,46};
twoTree tree = new twoTree();
tree.readDatas(data);
tree.printTree();
}
}