数据结构—树(二叉树)
一、二叉树的数据结构
public class TNode < E> {
private E data;
private TNode< E> lChild;
private TNode< E> rChild;
public TNode ( ) {
}
public TNode ( E data) {
this . data = data;
}
public TNode ( E data, TNode< E> lChild, TNode< E> rChild) {
this . data = data;
this . lChild = lChild;
this . rChild = rChild;
}
public E getData ( ) {
return data;
}
public void setData ( E data) {
this . data = data;
}
public TNode< E> getlChild ( ) {
return lChild;
}
public void setlChild ( TNode< E> lChild) {
this . lChild = lChild;
}
public TNode< E> getrChild ( ) {
return rChild;
}
public void setrChild ( TNode< E> rChild) {
this . rChild = rChild;
}
@Override
public String toString ( ) {
return "TNode{" +
"data=" + data +
", lChild=" + lChild +
", rChild=" + rChild +
'}' ;
}
}
二、二叉树算法实现(链式存储)
public class BinaryTree < E> {
public TNode< E> head;
public List< TNode> nodeList = null;
public BinaryTree ( ) {
head = null;
}
public BinaryTree ( E val, TNode< E> lp, TNode< E> rp) {
TNode< E> tNode = new TNode < E> ( val, lp, rp) ;
head = tNode;
}
public BinaryTree ( E val) {
this ( val, null, null) ;
}
public Boolean isEmpty ( ) {
return head == null;
}
public TNode< E> Root ( ) {
return head;
}
public TNode< E> getLChild ( TNode< E> tNode) {
return tNode. getlChild ( ) ;
}
public TNode< E> getRChild ( TNode< E> tNode) {
return tNode. getrChild ( ) ;
}
public void create ( E val, TNode< E> l, TNode< E> r) {
TNode< E> tNode = new TNode < > ( val, l, r) ;
head = tNode;
}
public void insertL ( E val, TNode< E> l) {
TNode< E> tNode = new TNode < > ( val) ;
tNode. setlChild ( l. getlChild ( ) ) ;
l. setlChild ( tNode) ;
}
public void insertR ( E val, TNode< E> r) {
TNode< E> tNode = new TNode < > ( val) ;
tNode. setrChild ( r. getrChild ( ) ) ;
r. setrChild ( tNode) ;
}
public TNode< E> deleteL ( TNode< E> p) {
if ( p == null || p. getlChild ( ) == null) {
return null;
}
TNode< E> tNode = new TNode < > ( ) ;
p. setlChild ( null) ;
return tNode;
}
public TNode< E> deleteR ( TNode< E> p) {
if ( p == null || p. getrChild ( ) == null) {
return null;
}
TNode< E> tNode = new TNode < > ( ) ;
p. setrChild ( null) ;
return tNode;
}
public TNode< E> search ( TNode< E> root, E val) {
TNode< E> p = root;
if ( p== null) {
return null;
}
if ( p. getData ( ) . equals ( val) ) {
return p;
}
if ( p. getlChild ( ) != null) {
return search ( p. getlChild ( ) , val) ;
}
if ( p. getrChild ( ) != null) {
return search ( p. getrChild ( ) , val) ;
}
return null;
}
public Boolean isLeaf ( TNode< E> p) {
return p!= null && p. getlChild ( ) == null && p. getrChild ( ) == null;
}
public void preorder ( TNode< E> p) {
if ( p == null)
return ;
System. out. print ( p. getData ( ) + " " ) ;
preorder ( p. getlChild ( ) ) ;
preorder ( p. getrChild ( ) ) ;
}
public void inorder ( TNode< E> p) {
if ( p == null)
return ;
inorder ( p. getlChild ( ) ) ;
System. out. print ( p. getData ( ) + " " ) ;
inorder ( p. getrChild ( ) ) ;
}
public void postorder ( TNode< E> p) {
if ( p == null)
return ;
postorder ( p. getlChild ( ) ) ;
postorder ( p. getrChild ( ) ) ;
System. out. print ( p. getData ( ) + " " ) ;
}
public void preorder_2 ( TNode< E> p) {
if ( p == null)
return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
stack. push ( p) ;
while ( ! stack. empty ( ) ) {
p = stack. pop ( ) ;
System. out. print ( p. getData ( ) + " " ) ;
if ( p. getrChild ( ) != null) stack. push ( p. getrChild ( ) ) ;
if ( p. getlChild ( ) != null) stack. push ( p. getlChild ( ) ) ;
}
}
public void preorder_3 ( TNode< E> p) {
if ( p == null)
return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
while ( ! stack. isEmpty ( ) || p!= null) {
while ( p!= null) {
System. out. print ( p. getData ( ) + " " ) ;
stack. push ( p) ;
p = p. getlChild ( ) ;
}
p = stack. pop ( ) ;
p = p. getrChild ( ) ;
}
}
public void inorder_2 ( TNode< E> p) {
if ( p == null)
return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
while ( ! stack. isEmpty ( ) || p!= null) {
while ( p!= null) {
stack. push ( p) ;
p = p. getlChild ( ) ;
}
p = stack. pop ( ) ;
System. out. print ( p. getData ( ) + " " ) ;
p = p. getrChild ( ) ;
}
}
public void postorder_2 ( TNode< E> p) {
if ( p == null)
return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
TNode< E> prep = p;
while ( ! stack. isEmpty ( ) || p!= null) {
while ( p != null) {
stack. push ( p) ;
p = p. getlChild ( ) ;
}
p = stack. peek ( ) . getrChild ( ) ;
if ( p== null || p == prep) {
p = stack. pop ( ) ;
System. out. print ( p. getData ( ) + " " ) ;
prep = p;
p = null;
}
}
}
public void postorder_3 ( TNode< E> p) {
if ( p == null)
return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
TNode< E> prep = p;
while ( ! stack. isEmpty ( ) || p!= null) {
while ( p. getlChild ( ) != null) {
stack. push ( p) ;
p = p. getlChild ( ) ;
}
while ( p!= null && ( p. getrChild ( ) == null|| p. getrChild ( ) == prep) ) {
System. out. print ( p. getData ( ) + " " ) ;
prep = p;
if ( stack. empty ( ) ) return ;
p = stack. pop ( ) ;
}
stack. push ( p) ;
p = p. getrChild ( ) ;
}
}
public void postorder_4 ( TNode< E> p) {
if ( p == null) return ;
Stack< TNode< E> > stack = new Stack < > ( ) ;
Stack< TNode< E> > result = new Stack < > ( ) ;
while ( ! stack. isEmpty ( ) || p!= null) {
while ( p!= null) {
stack. push ( p) ;
result. push ( p) ;
p = p. getrChild ( ) ;
}
if ( ! stack. empty ( ) )
p = stack. pop ( ) . getlChild ( ) ;
}
while ( ! result. empty ( ) ) {
p = result. pop ( ) ;
System. out. print ( p. getData ( ) + " " ) ;
}
}
public void levelOrder ( TNode< E> root) {
if ( root == null)
return ;
Queue< TNode< E> > queue = new LinkedList < TNode< E> > ( ) ;
queue. add ( root) ;
while ( ! queue. isEmpty ( ) ) {
TNode< E> temp = queue. poll ( ) ;
System. out. print ( temp. getData ( ) + " " ) ;
if ( temp. getlChild ( ) != null) {
queue. add ( temp. getlChild ( ) ) ;
}
if ( temp. getrChild ( ) != null) {
queue. add ( temp. getrChild ( ) ) ;
}
}
}
}
三、测试
public class Test {
public static void main ( String[ ] args) {
BinaryTree< String> binaryTree = new BinaryTree < > ( "A" ) ;
System. out. println ( "根节点" + binaryTree. head. getData ( ) ) ;
binaryTree. insertL ( "B" , binaryTree. head) ;
binaryTree. insertR ( "C" , binaryTree. head) ;
binaryTree. insertL ( "D" , binaryTree. head. getlChild ( ) ) ;
binaryTree. insertL ( "E" , binaryTree. head. getrChild ( ) ) ;
binaryTree. insertR ( "F" , binaryTree. head. getrChild ( ) ) ;
binaryTree. preorder_3 ( binaryTree. head) ;
}
}