java数据结构----二叉树的实现
代码实现:
定义节点:
public class Node {
private int data;
private Node leftChild;
private Node rightChild;
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public void setData(int data) {
this.data = data;
}
public Node getRightChild() {
return rightChild;
}
public Node getLeftChild() {
return leftChild;
}
public int getData() {
return data;
}
public Node(int data)
{
this.data=data;
}
}
二叉树的实现:
import java.util.LinkedList;
public class Tree {
public Node getRoot() {
return root;
}
private Node root;
public void insert(int data)
{
Node newNode=new Node(data);
Node current=root;
Node parent;
if(root==null)
{
root=newNode;
}else
{
while(true)
{
parent=current;
if(current.getData()>data)
{
current=current.getLeftChild();
if(current==null)
{
parent.setLeftChild(newNode);
return ;
}
}else
{
current=current.getRightChild();
if(current==null)
{
parent.setRightChild(newNode);
return ;
}
}
}
}
}
public Node find(int data)
{
Node current=root;
while(current.getData()!=data)
{
if(current.getData()>data)
{
current=current.getLeftChild();
}else
{
current=current.getRightChild();
}
if(current==null)
return null;
}
return current;
}
public Node getSuccessor(Node delNode)
{
Node successor=delNode;
Node successorParent=delNode;
Node current=delNode.getRightChild();
while(current!=null)
{
successorParent=successor;
successor=current;
current=current.getLeftChild();
}
if(successor!=delNode.getRightChild())
{
successorParent.setLeftChild(successor.getRightChild());
successor.setRightChild(delNode.getRightChild());
}
return successor;
}
/**
* 先序遍历
*/
public void frontOrder(Node root)
{
if(root!=null)
{
System.out.print(root.getData()+" ");
frontOrder(root.getLeftChild());
frontOrder(root.getRightChild());
}
}
/**
* 中序遍历
*/
public void middleOrder(Node root)
{
if(root!=null)
{
middleOrder(root.getLeftChild());
System.out.print(root.getData()+" ");
middleOrder(root.getRightChild());
}
}
/**
* 后序遍历
*/
public void afterOrder(Node root)
{
if(root!=null)
{
afterOrder(root.getLeftChild());
afterOrder(root.getRightChild());
System.out.print(root.getData()+" ");
}
}
/**
* 删除节点
*/
public boolean delete(int data)
{
Node current=root;
Node parent=root;
boolean isLeftChild=true;
while(current.getData()!=data)
{
parent=current;
if(current.getData()>data)
{
current=current.getLeftChild();
}else
{
current=current.getRightChild();
isLeftChild=false;
}
if(current==null)
return false;
}
//删除叶子节点
if(current.getLeftChild()==null&¤t.getRightChild()==null)
{
if(current==root)
{
root=null;
}
if(isLeftChild) {
parent.setLeftChild(null) ;
}else
{
parent.setRightChild(null);
}
}else if(current.getRightChild()==null)
{
if(current==root)
{
root=current.getLeftChild();
}
if(isLeftChild)
{
parent.setLeftChild(current.getLeftChild());
}else
{
parent.setRightChild(current.getLeftChild());
}
}else if(current.getLeftChild()==null)
{
if(current==root)
{
root=current.getRightChild();
}
if(isLeftChild)
{
parent.setLeftChild(current.getRightChild());
}else
{
parent.setRightChild(current.getRightChild());
}
}else
{
Node successor=getSuccessor(current);
if(current==root)
{
root=successor;
}else if(isLeftChild)
{
parent.setLeftChild(successor);
}else
{
parent.setRightChild(successor);
}
successor.setLeftChild(current.getLeftChild());
}
return true;
}
/**
* 树的层次遍历
* @param root
*/
public void LaywerTraversal(Node root){
if(root==null) return;
LinkedList<Node> list = new LinkedList<Node>();
list.add(root);
Node currentNode;
while(!list.isEmpty()){
currentNode=list.poll();
System.out.println(currentNode.getData());
if(currentNode.getLeftChild()!=null){
list.add(currentNode.getLeftChild());
}
if(currentNode.getRightChild()!=null){
list.add(currentNode.getRightChild());
}
}
}
}