二叉树类
public class BinaryNode < T > {
public T data;
public BinaryNode < T > left, right;
public BinaryNode ( T data, BinaryNode < T > left, BinaryNode < T > right) {
this . data= data;
this . left= left;
this . right= right;
}
public BinaryNode ( T data) {
this . data= data;
this . left= null ;
this . right= null ;
}
public String toString ( ) {
return data. toString ( ) ;
}
public boolean isLeaf ( ) {
if ( this . left== null && this . right== null ) {
return true ;
}
else
return false ;
}
}
public class BinaryTree < T > {
public BinaryNode < T > root;
public BinaryTree ( ) {
this . root = null ;
}
public BinaryTree ( T [ ] preList, T [ ] inList, int preIndex, int inIndex, int count) {
if ( count> 0 ) {
T rootVal= preList[ preIndex] ;
int i= 0 ;
for ( ; i< count; i++ ) {
if ( rootVal== inList[ i+ inIndex] ) {
break ;
}
}
this . root= new BinaryNode < > ( rootVal) ;
this . root. left= new BinaryTree < > ( preList, inList, preIndex+ 1 , inIndex, i) . root;
this . root. right= new BinaryTree < > ( preList, inList, preIndex+ i+ 1 , inIndex+ i+ 1 , count- i- 1 ) . root;
}
}
public BinaryTree ( T [ ] preList) {
this . root= create ( preList) ;
}
private int i= 0 ;
private BinaryNode < T > create ( T [ ] preList) {
BinaryNode < T > p= null ;
if ( i< preList. length) {
T elem= preList[ i++ ] ;
if ( elem!= null ) {
p= new BinaryNode < > ( elem) ;
p. left= create ( preList) ;
p. right= create ( preList) ;
}
}
return p;
}
public boolean isEmpty ( ) {
return this . root == null ;
}
public void insert ( T x) {
if ( x != null ) {
this . root = new BinaryNode < T > ( x, this . root, null ) ;
}
}
public BinaryNode < T > insert ( BinaryNode < T > p, boolean left, T x) {
if ( x == null || p == null ) {
return null ;
}
if ( left) {
return p. left = new BinaryNode < > ( x, p. left, null ) ;
}
return p. right = new BinaryNode < > ( x, null , p. right) ;
}
public void remove ( BinaryNode < T > p, boolean left) {
if ( p != null ) {
if ( left) {
p. left = null ;
} else {
p. right = null ;
}
}
}
public void clear ( ) {
this . root = null ;
}
public void preOrder ( ) {
preOrder ( this . root) ;
System . out. println ( ) ;
}
public void preOrder ( BinaryNode < T > p) {
if ( p!= null ) {
System . out. print ( p. data. toString ( ) + " " ) ;
preOrder ( p. left) ;
preOrder ( p. right) ;
}
}
public void inOrder ( ) {
inOrder ( this . root) ;
System . out. println ( ) ;
}
public void inOrder ( BinaryNode < T > p) {
if ( p!= null ) {
inOrder ( p. left) ;
System . out. print ( p. data. toString ( ) + " " ) ;
inOrder ( p. right) ;
}
}
public void postOrder ( ) {
preOrder ( this . root) ;
System . out. println ( ) ;
}
public void postOrder ( BinaryNode < T > p) {
if ( p!= null ) {
preOrder ( p. left) ;
preOrder ( p. right) ;
System . out. print ( p. data. toString ( ) + " " ) ;
}
}
public String toGenListString ( ) {
return "二叉树的广义表表示: " + toGenListString ( this . root) ;
}
public String toGenListString ( BinaryNode < T > p) {
if ( p== null ) {
return "^" ;
}
if ( p. left== null && p. right== null ) {
return p. data. toString ( ) ;
}
return p. data. toString ( ) + "(" + toGenListString ( p. left) + "," + toGenListString ( p. right) + ")" ;
}
public void preOrderTraverse ( ) {
Stack < BinaryNode < T > > stack= new LinkedStack < > ( ) ;
BinaryNode < T > p= this . root;
while ( p!= null || ! stack. isEmpty ( ) ) {
if ( p!= null ) {
System . out. print ( p. data + " " ) ;
stack. push ( p) ;
p= p. left;
}
else {
System . out. print ( "^ " ) ;
p= stack. pop ( ) ;
p= p. right;
}
}
System . out. println ( "^" ) ;
}
public void inOrderTraverse ( ) {
Stack < BinaryNode < T > > stack= new LinkedStack < > ( ) ;
BinaryNode < T > p= this . root;
while ( p!= null || ! stack. isEmpty ( ) ) {
if ( p!= null ) {
stack. push ( p) ;
p= p. left;
}
else {
System . out. print ( "^ " ) ;
p= stack. pop ( ) ;
System . out. print ( p. data + " " ) ;
p= p. right;
}
}
System . out. println ( "^" ) ;
}
public void postOrderTraverse ( ) {
Stack < BinaryNode < T > > stack= new LinkedStack < > ( ) ;
BinaryNode < T > last= null ;
BinaryNode < T > p= this . root;
while ( p!= null || ! stack. isEmpty ( ) ) {
while ( p!= null ) {
stack. push ( p) ;
p= p. left;
}
if ( ! stack. isEmpty ( ) ) {
BinaryNode < T > t= stack. pop ( ) ;
if ( t. right== null || last== t. right) {
System . out. print ( t. data + " " ) ;
last= t;
}
else {
stack. push ( t) ;
p= t. right;
}
}
}
}
public void postOrderTraverse2 ( ) {
Stack < BinaryNode < T > > stack= new LinkedStack < > ( ) ;
BinaryNode < T > last= null ;
BinaryNode < T > p= this . root;
while ( p!= null || ! stack. isEmpty ( ) ) {
if ( p!= null ) {
stack. push ( p) ;
p= p. left;
}
else {
BinaryNode < T > t= stack. pop ( ) ;
if ( t. right== null ) {
System . out. print ( t. data + " " ) ;
last= t;
}
else if ( last== t. right) {
System . out. print ( t. data + " " ) ;
last= t;
}
else {
stack. push ( t) ;
p= t. right;
}
}
}
}
public void levelOrder ( ) {
if ( this . root== null ) {
return ;
}
Queue < BinaryNode < T > > que= new LinkedQueue < > ( ) ;
que. add ( this . root) ;
while ( ! que. isEmpty ( ) ) {
BinaryNode < T > p= que. poll ( ) ;
System . out. print ( p. data + " " ) ;
if ( p. left!= null ) {
que. add ( p. left) ;
}
if ( p. right!= null ) {
que. add ( p. right) ;
}
}
System . out. println ( ) ;
}
public static void main ( String [ ] args) {
String [ ] preList= { "A" , "B" , "D" , null , "G" , null , null , null , "C" , "E" , null , null , "F" , "H" } ;
BinaryTree < String > binaryTree= new BinaryTree < > ( preList) ;
binaryTree. preOrderTraverse ( ) ;
binaryTree. postOrderTraverse2 ( ) ;
}
}
public class BinaryTrees {
private static int i= 0 ;
public static BinaryTree < String > createByGenList ( String genList) {
BinaryTree < String > binaryTree= new BinaryTree < > ( ) ;
i= 0 ;
binaryTree. root= create ( genList) ;
return binaryTree;
}
public static BinaryNode < String > create ( String genList) {
BinaryNode < String > p= null ;
if ( i< genList. length ( ) ) {
char ch= genList. charAt ( i) ;
i++ ;
if ( ch== '^' ) {
return null ;
}
p= new BinaryNode < > ( ch + "" ) ;
if ( i< genList. length ( ) && genList. charAt ( i) == '(' ) {
p. left= create ( genList) ;
i++ ;
p. right= create ( genList) ;
i++ ;
}
}
return p;
}
}