关于树的基本概念可以查看此篇文章树、堆、集合
1、一般树的实现:
树结构可以由递归实现,也可以由链表实现:
链表实现的单个节点的表示方法:
class TreeNode{
Object element; //节点的值
TreeNode firstChild; //该节点的第一个子节点
TreeNode nextSibling; //下一个兄弟节点
}
树的遍历:分为前序遍历、后续遍历,二叉树还有一种特殊的遍历方法(中序遍历);
前序遍历:及对节点的处理,先于其子节点进行,其时间复杂度为O(n);
后序遍历:及对节点的处理,晚于其子节点进行,其时间复杂度为O(n);
[图片上传失败...(image-754579-1509972359621)]
前序遍历为:A-B-D-#-#-#-C-E-#-#-#
后序遍历为:#-#-D-#-B-#-#-E-#-C-A
2、二叉树的实现:
二叉树是一颗树,其每个节点都不能有多于两个的儿子;
其单个节点为:
private static class BinaryNode<T>{
T element;
BinaryNode<T> leftBinaryNode;
BinaryNode<T> rightBinaryNode;
BinaryNode(T element){
this.element=element;
}
public BinaryNode(T element, BinaryNode<T> leftBinaryNode, BinaryNode<T> rightBinaryNode) {
this.element = element;
this.leftBinaryNode = leftBinaryNode;
this.rightBinaryNode = rightBinaryNode;
}
}
二叉搜索树的实现:
package tree;
import com.sun.jmx.remote.internal.ArrayQueue;
import java.nio.BufferUnderflowException;
import java.util.Queue;
public class myBinaryTree<T extends Comparable<? super T>> {
private BinaryNode<T> root;
public myBinaryTree(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
public void makeEmpty(){
root=null;
}
public boolean contains(T item){
return contains(item,root);
}
private boolean contains(T item,BinaryNode<T> t){
if(t==null){
return false;
}
int compareValue=item.compareTo(t.element);
if(compareValue<0){
return contains(item,t.leftBinaryNode);
}
else if(compareValue>0){
return contains(item,t.rightBinaryNode);
}
else {
return true;
}
}
public T findMin(){
if(isEmpty())
throw new BufferUnderflowException();
return findMin(root).element;
}
private BinaryNode<T> findMin(BinaryNode<T> t){
if(t==null){
return null;
}
while (t.leftBinaryNode!=null){
t=t.leftBinaryNode;
}
return t;
}
public T findMax(){
if(isEmpty())
throw new BufferUnderflowException();
return findMax(root).element;
}
private BinaryNode<T> findMax(BinaryNode<T> t){
if(t==null){
return null;
}
while (t.rightBinaryNode!=null){
t=t.rightBinaryNode;
}
return t;
}
public void insert(T item){
root=insert(item,root);
}
private BinaryNode<T> insert(T item,BinaryNode<T> t){
if(t==null){
return new BinaryNode<T>(item,null,null);
}
int compareValue=item.compareTo(t.element);
if(compareValue<0){
t.leftBinaryNode=insert(item,t.leftBinaryNode);
}
else if(compareValue>0){
t.rightBinaryNode=insert(item,t.rightBinaryNode);
}
else{
;
}
return t;
}
public int height(){
if(isEmpty()){
return 0;
}
else
return height(root);
}
private int height(BinaryNode<T> t){
if(t==null){
return 0;
}
else
return 1+Math.max(height(t.rightBinaryNode),height(t.leftBinaryNode));
}
public void printTreeNodeLast(){ //后序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTreeNodeLast(root);
}
}
private void printTreeNodeLast(BinaryNode<T> t){
if (t!=null){
printTreeNodeLast(t.leftBinaryNode);
printTreeNodeLast(t.rightBinaryNode);
System.out.println(t.element);
}
}
public void printTreeNodeFirst(){ //先序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTreeNodeFirst(root);
}
}
private void printTreeNodeFirst(BinaryNode<T> t){
if (t!=null){
System.out.println(t.element);
printTreeNodeFirst(t.leftBinaryNode);
printTreeNodeFirst(t.rightBinaryNode);
}
}
public void printTreeNodeMedu(){ //中序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTreeNodeMedu(root);
}
}
private void printTreeNodeMedu(BinaryNode<T> t){
if (t!=null){
printTreeNodeMedu(t.leftBinaryNode);
System.out.print(t.element);
printTreeNodeMedu(t.rightBinaryNode);
}
}
public void printTree(){ //层序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTree(root);
}
}
private void printTree(BinaryNode<T> t){
ArrayQueue<BinaryNode<T>> arrayQueue=new ArrayQueue<>(height());
if(t==null){
System.out.println("Tree is empty");
}
arrayQueue.add(t);
while (!arrayQueue.isEmpty()){
BinaryNode<T> binaryNode=arrayQueue.remove(0);
System.out.println(binaryNode.element);
if(binaryNode.leftBinaryNode!=null){
arrayQueue.add(binaryNode.leftBinaryNode);
}
if(binaryNode.rightBinaryNode!=null){
arrayQueue.add(binaryNode.rightBinaryNode);
}
}
}
public void remove(T item){
root=remove(item,root);
}
private BinaryNode<T> remove(T item,BinaryNode<T> t){
if(t==null){
return null;
}
int comparaValue=item.compareTo(t.element);
if(comparaValue>0){
t.rightBinaryNode=remove(item,t.rightBinaryNode);
}
else if(comparaValue<0) {
t.leftBinaryNode=remove(item,t.leftBinaryNode);
}
else {
if(t.rightBinaryNode!=null&&t.leftBinaryNode!=null){
t.element=findMin(t.rightBinaryNode).element;
t.rightBinaryNode=remove(t.element,t.rightBinaryNode);
}
else {
t=(t.leftBinaryNode!=null)?t.leftBinaryNode:t.rightBinaryNode;
}
}
return t;
}
private static class BinaryNode<T>{
T element;
BinaryNode<T> leftBinaryNode;
BinaryNode<T> rightBinaryNode;
BinaryNode(T element){
this.element=element;
}
public BinaryNode(T element, BinaryNode<T> leftBinaryNode, BinaryNode<T> rightBinaryNode) {
this.element = element;
this.leftBinaryNode = leftBinaryNode;
this.rightBinaryNode = rightBinaryNode;
}
}
}
3、AVL树
带有自平衡调整的二叉查找树,其要求每个节点的左右子节点的高度差,相差不超过1;
package tree;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;
import java.nio.BufferUnderflowException;
public class myAvlTree<T extends Comparable<? super T>> {
private static final int ALLOWED_HEIGHT=1;
private AvlNode<T> root;
public myAvlTree(){
root=null;
}
public boolean isEmpty(){
return root==null;
}
public void insert(T item){
root=insert(item,root);
}
public T findMin(){
if(isEmpty())
throw new BufferUnderflowException();
return findMin(root).element;
}
private AvlNode<T> findMin(AvlNode<T> t){
if(t==null){
return null;
}
while (t.left!=null){
t=t.left;
}
return t;
}
private AvlNode<T> insert(T item,AvlNode<T> t){
if(t==null){
return new AvlNode<T>(item,null,null);
}
int comparaValue=item.compareTo(t.element);
if(comparaValue>0){
t.right=insert(item,t.right);
}
else if(comparaValue<0){
t.left=insert(item,t.left);
}
else
;
return balance(t);
}
public void remove(T x){
if(isEmpty()){
System.out.println("tree is empty");
throw new BufferUnderflowException();
}
remove(x,root);
}
private AvlNode<T> remove(T x,AvlNode<T> t){
if(t==null){
return null;
}
int comparaVaule=x.compareTo(t.element);
if(comparaVaule>0){
t.right=remove(x,t.right);
}
else if(comparaVaule<0){
t.left=remove(x,t.left);
}
else if(t.left!=null&&t.right!=null){
t.element=findMin(t.right).element;
t.right=remove(t.element,t.left);
}
else {
t=(t.left!=null)?t.left:t.right;
}
return t;
}
private AvlNode<T> balance(AvlNode<T> t){
if(t==null){
return t;
}
if(height(t.left)-height(t.right)>ALLOWED_HEIGHT){
if(height(t.left.left)>=height(t.left.right)){
t=rotateWithLeftChild(t);
}
else{
t=doubleWithLeftChild(t);
}
}
else if(height(t.right)-height(t.left)>ALLOWED_HEIGHT){
if(height(t.right.right)>=height(t.right.left)){
t=rotateWithRightChild(t);
}
else {
t=doubleWithRightChild(t);
}
}
t.height=Math.max(height(t.left),height(t.right))+1;
return t;
}
private AvlNode<T> rotateWithLeftChild(AvlNode<T> k2){
AvlNode<T> k1=k2.left;
k2.left=k1.right;
k1.right=k2;
k2.height= Math.max(height(k2.left),height(k2.right))+1;
k1.height=Math.max(height(k1.left),k2.height)+1;
return k1;
}
private AvlNode<T> doubleWithLeftChild(AvlNode<T> k2){
k2.left=rotateWithRightChild(k2.left);
return rotateWithLeftChild(k2);
}
private AvlNode<T> rotateWithRightChild(AvlNode<T> k1){
AvlNode<T> k2=k1.right;
k1.right=k2.left;
k2.left=k1;
k1.height=Math.max(height(k1.right),k1.height);
k2.height=Math.max(k1.height,height(k2.right))+1;
return k2;
}
private AvlNode<T> doubleWithRightChild(AvlNode<T> k2){
k2.right=rotateWithLeftChild(k2.left);
return rotateWithRightChild(k2);
}
private int height(AvlNode<T> t){ //计算AVL树的高度;
return t==null?-1:t.height;
}
public void printTree(){ //后序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTree(root);
}
}
private void printTree(AvlNode<T> t){
if (t!=null){
printTree(t.left);
printTree(t.right);
System.out.println(t.element);
}
}
public void printTreelast(){ //先序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTreelast(root);
}
}
private void printTreelast(AvlNode<T> t){
if (t!=null){
System.out.println(t.element);
printTreelast(t.left);
printTreelast(t.right);
}
}
public void printTreemd(){ //中序遍历
if(isEmpty()){
System.out.println("Tree is empty");
}
else {
printTreemd(root);
}
}
private void printTreemd(AvlNode<T> t){
if (t!=null){
printTreemd(t.left);
System.out.println(t.element);
printTreemd(t.right);
}
}
private static class AvlNode<T>{
T element;
AvlNode<T> left;
AvlNode<T> right;
int height; //height
AvlNode(T element){
this.element=element;
}
public AvlNode(T element, AvlNode<T> left, AvlNode<T> right) {
this.element = element;
this.left = left;
this.right = right;
height=0;
}
}
}
public static void main(String[] args){
myAvlTree<Integer> myAvlTree=new myAvlTree<>();
Integer[] integer={10,3,5,9,8,7,0,4};
for(Integer ints:integer){
myAvlTree.insert(ints);
}
System.out.println("中序");
myAvlTree.printTreemd();
}
中序
0
3
4
5
7
8
9
10