手动创建一个二叉树 如图
代码
public class Main {
public static void main ( String [ ] args) {
HeroNode heroNode1 = new HeroNode ( 1 , "宋江" ) ;
HeroNode heroNode2 = new HeroNode ( 2 , "吴用" ) ;
HeroNode heroNode3 = new HeroNode ( 3 , "卢俊义" ) ;
HeroNode heroNode4 = new HeroNode ( 4 , "关胜" ) ;
HeroNode heroNode5 = new HeroNode ( 5 , "林冲" ) ;
BinaryTree binaryTree = new BinaryTree ( ) ;
binaryTree. root = heroNode1;
heroNode1. left = heroNode2;
heroNode1. right = heroNode3;
heroNode3. left = heroNode4;
heroNode3. right = heroNode5;
System . out. println ( "------前序 中序 后序遍历-------" ) ;
binaryTree. preOrder ( ) ;
binaryTree. infixOrder ( ) ;
binaryTree. postOrder ( ) ;
System . out. println ( "------------------------------" ) ;
System . out. println ( "------查找------" ) ;
System . out. println ( binaryTree. preOrderSearch ( 2 ) ) ;
System . out. println ( binaryTree. infixOrderSearch ( 9 ) ) ;
System . out. println ( "------------------------------" ) ;
System . out. println ( "------删除------" ) ;
binaryTree. deleteNode ( 3 ) ;
binaryTree. preOrder ( ) ;
}
}
class BinaryTree {
HeroNode root;
public void deleteNode ( int id) {
if ( root == null ) {
System . out. println ( "根节点为null 无法删除" ) ;
} else {
if ( root. id == id) {
root = null ;
} else {
root. deleteNode ( id) ;
}
}
}
public void preOrder ( ) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return ;
}
root. preOrder ( ) ;
}
public void infixOrder ( ) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return ;
}
root. infixOrder ( ) ;
}
public void postOrder ( ) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return ;
}
root. postOrder ( ) ;
}
public HeroNode preOrderSearch ( int id) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return null ;
} else {
return root. preOrderSearch ( id) ;
}
}
public HeroNode infixOrderSearch ( int id) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return null ;
} else {
return root. infixOrderSearch ( id) ;
}
}
public HeroNode postOrderSearch ( int id) {
if ( root == null ) {
System . out. println ( "二叉树为空 无法遍历" ) ;
return null ;
} else {
return root. postOrderSearch ( id) ;
}
}
}
class HeroNode {
int id;
String name;
HeroNode left;
HeroNode right;
public HeroNode ( int id, String name) {
this . id = id;
this . name = name;
}
public void deleteNode ( int id) {
if ( this . left != null && this . left. id == id) {
this . left = null ;
return ;
}
if ( this . right != null && this . right. id == id) {
this . right = null ;
return ;
}
if ( this . left != null ) {
this . left. deleteNode ( id) ;
}
if ( this . right != null ) {
this . right. deleteNode ( id) ;
}
}
public void preOrder ( ) {
System . out. println ( this ) ;
if ( this . left != null ) {
this . left. preOrder ( ) ;
}
if ( this . right != null ) {
this . right. preOrder ( ) ;
}
}
public void infixOrder ( ) {
if ( this . left != null ) {
this . left. infixOrder ( ) ;
}
System . out. println ( this ) ;
if ( this . right != null ) {
this . right. infixOrder ( ) ;
}
}
public void postOrder ( ) {
if ( this . left != null ) {
this . left. postOrder ( ) ;
}
if ( this . right != null ) {
this . right. postOrder ( ) ;
}
System . out. println ( this ) ;
}
public HeroNode preOrderSearch ( int id) {
if ( this . id == id) {
return this ;
}
HeroNode heroNode = null ;
if ( this . left != null ) {
heroNode = this . left. preOrderSearch ( id) ;
}
if ( heroNode != null ) {
return heroNode;
}
if ( this . right != null ) {
heroNode = this . right. preOrderSearch ( id) ;
}
return heroNode;
}
public HeroNode infixOrderSearch ( int id) {
HeroNode heroNode = null ;
if ( this . left != null ) {
heroNode = this . left. infixOrderSearch ( id) ;
}
if ( heroNode != null ) {
return heroNode;
}
if ( this . id == id) {
return this ;
}
if ( this . right != null ) {
heroNode = this . right. infixOrderSearch ( id) ;
}
return heroNode;
}
public HeroNode postOrderSearch ( int id) {
HeroNode heroNode = null ;
if ( this . left != null ) {
heroNode = this . left. postOrderSearch ( id) ;
}
if ( heroNode != null ) {
return heroNode;
}
if ( this . right != null ) {
heroNode = this . right. postOrderSearch ( id) ;
}
if ( heroNode != null ) {
return heroNode;
}
if ( this . id == id) {
return this ;
}
return null ;
}
@Override
public String toString ( ) {
return "HeroNode{" +
"id=" + id +
", name='" + name + '\'' +
'}' ;
}
}
结果
-- -- -- 前序 中序 后序遍历-- -- -- -
HeroNode { id= 1 , name= '宋江' }
HeroNode { id= 2 , name= '吴用' }
HeroNode { id= 3 , name= '卢俊义' }
HeroNode { id= 4 , name= '关胜' }
HeroNode { id= 5 , name= '林冲' }
HeroNode { id= 2 , name= '吴用' }
HeroNode { id= 1 , name= '宋江' }
HeroNode { id= 4 , name= '关胜' }
HeroNode { id= 3 , name= '卢俊义' }
HeroNode { id= 5 , name= '林冲' }
HeroNode { id= 2 , name= '吴用' }
HeroNode { id= 4 , name= '关胜' }
HeroNode { id= 5 , name= '林冲' }
HeroNode { id= 3 , name= '卢俊义' }
HeroNode { id= 1 , name= '宋江' }
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- 查找-- -- --
HeroNode { id= 2 , name= '吴用' }
null
-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- -- -- 删除-- -- --
HeroNode { id= 1 , name= '宋江' }
HeroNode { id= 2 , name= '吴用' }