二叉树
package com. weeks. tree ;
public class BinaryTreeDemo {
public static void main ( String [ ] args) {
BinaryTree binaryTree = new BinaryTree ( ) ;
HeroNode node1 = new HeroNode ( 1 , "宋江" ) ;
HeroNode node2 = new HeroNode ( 2 , "吴用" ) ;
HeroNode node3 = new HeroNode ( 3 , "卢俊义" ) ;
HeroNode node4 = new HeroNode ( 4 , "林冲" ) ;
HeroNode node5 = new HeroNode ( 5 , "关胜" ) ;
binaryTree. setRoot ( node1) ;
node1. setLeft ( node2) ;
node1. setRight ( node3) ;
node3. setRight ( node4) ;
node3. setLeft ( node5) ;
System . out. println ( "删除前的二叉树(前序遍历)" ) ;
binaryTree. preOrder ( ) ;
binaryTree. delNode ( 5 ) ;
System . out. println ( "删除后的二叉树(前序遍历)" ) ;
binaryTree. preOrder ( ) ;
}
}
class BinaryTree {
private HeroNode root;
public HeroNode getRoot ( ) {
return root;
}
public void setRoot ( HeroNode root) {
this . root = root;
}
public void preOrder ( ) {
if ( root == null ) {
return ;
}
root. preOrder ( ) ;
}
public void infixOrder ( ) {
if ( root == null ) {
return ;
}
root. infixOrder ( ) ;
}
public void postOrder ( ) {
if ( root == null ) {
return ;
}
root. postOrder ( ) ;
}
public HeroNode preOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. preOrderSearch ( no) ;
}
public HeroNode infixOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. infixOrderSearch ( no) ;
}
public HeroNode postOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. postOrderSearch ( no) ;
}
public void delNode ( int no) {
if ( root == null ) {
return ;
}
if ( root. getNo ( ) == no) {
root = null ;
return ;
} else {
root. delNode ( no) ;
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode ( int no, String name) {
this . no = no;
this . name = name;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public HeroNode getLeft ( ) {
return left;
}
public void setLeft ( HeroNode left) {
this . left = left;
}
public HeroNode getRight ( ) {
return right;
}
public void setRight ( HeroNode right) {
this . right = right;
}
@Override
public String toString ( ) {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}' ;
}
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 no) {
if ( this . no == no) {
return this ;
}
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. preOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . right != null ) {
resNode = this . right. preOrderSearch ( no) ;
}
return resNode;
}
public HeroNode infixOrderSearch ( int no) {
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. infixOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . no == no) {
return this ;
}
if ( this . right != null ) {
resNode = this . right. infixOrderSearch ( no) ;
}
return resNode;
}
public HeroNode postOrderSearch ( int no) {
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. postOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . right != null ) {
resNode = this . right. postOrderSearch ( no) ;
}
if ( this . no == no) {
return this ;
}
return resNode;
}
public void delNode ( int no) {
if ( this . left != null && this . left. no == no) {
this . left = null ;
return ;
}
if ( this . right != null && this . right. no == no) {
this . right = null ;
return ;
}
if ( this . left != null ) {
this . left. delNode ( no) ;
}
if ( this . right != null ) {
this . right. delNode ( no) ;
}
}
}
顺序存储二叉树
package com. weeks. tree ;
import java. util. ArrayList ;
public class ArrayBinaryTreeDemo {
public static void main ( String [ ] args) {
int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ;
ArrayBinaryTree arrayBinaryTree = new ArrayBinaryTree ( arr) ;
arrayBinaryTree. postOrder ( ) ;
}
}
class ArrayBinaryTree {
private int [ ] arr;
public ArrayBinaryTree ( int [ ] arr) {
this . arr = arr;
}
public void preOrder ( int index) {
if ( arr == null ) {
return ;
}
System . out. println ( arr[ index] ) ;
if ( ( 2 * index + 1 ) < arr. length) {
preOrder ( 2 * index + 1 ) ;
}
if ( ( 2 * index + 2 ) < arr. length) {
preOrder ( 2 * index + 2 ) ;
}
}
public void preOrder ( ) {
preOrder ( 0 ) ;
}
public void infixOrder ( int index) {
if ( arr == null ) {
return ;
}
if ( ( 2 * index + 1 ) < arr. length) {
infixOrder ( 2 * index + 1 ) ;
}
System . out. println ( arr[ index] ) ;
if ( ( 2 * index + 2 ) < arr. length) {
infixOrder ( 2 * index + 2 ) ;
}
}
public void infixOrder ( ) {
infixOrder ( 0 ) ;
}
public void postOrder ( int index) {
if ( arr == null ) {
return ;
}
if ( ( 2 * index + 1 ) < arr. length) {
postOrder ( 2 * index + 1 ) ;
}
if ( ( 2 * index + 2 ) < arr. length) {
postOrder ( 2 * index + 2 ) ;
}
System . out. println ( arr[ index] ) ;
}
public void postOrder ( ) {
postOrder ( 0 ) ;
}
}
线索二叉树
package com. weeks. tree. threadedbinarytree ;
public class ThreadedBinaryTreeDemo {
public static void main ( String [ ] args) {
HeroNode node1 = new HeroNode ( 1 , "tom" ) ;
HeroNode node2 = new HeroNode ( 3 , "jack" ) ;
HeroNode node3 = new HeroNode ( 6 , "smith" ) ;
HeroNode node4 = new HeroNode ( 8 , "milan" ) ;
HeroNode node5 = new HeroNode ( 10 , "adair" ) ;
HeroNode node6 = new HeroNode ( 14 , "john" ) ;
node1. setLeft ( node2) ;
node1. setRight ( node3) ;
node2. setLeft ( node4) ;
node2. setRight ( node5) ;
node3. setLeft ( node6) ;
ThreadedBinaryTree threadedBinaryTree = new ThreadedBinaryTree ( ) ;
threadedBinaryTree. setRoot ( node1) ;
HeroNode node5Left = node5. getLeft ( ) ;
HeroNode node5Right = node5. getRight ( ) ;
System . out. println ( "node5Left=" + node5Left) ;
System . out. println ( "node5Right=" + node5Right) ;
}
}
class ThreadedBinaryTree {
private HeroNode root;
private HeroNode pre;
public HeroNode getRoot ( ) {
return root;
}
public void setRoot ( HeroNode root) {
this . root = root;
}
public void preOrder ( ) {
if ( root == null ) {
return ;
}
root. preOrder ( ) ;
}
public void infixOrder ( ) {
if ( root == null ) {
return ;
}
root. infixOrder ( ) ;
}
public void postOrder ( ) {
if ( root == null ) {
return ;
}
root. postOrder ( ) ;
}
public HeroNode preOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. preOrderSearch ( no) ;
}
public HeroNode infixOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. infixOrderSearch ( no) ;
}
public HeroNode postOrderSearch ( int no) {
if ( root == null ) {
return null ;
}
return root. postOrderSearch ( no) ;
}
public void delNode ( int no) {
if ( root == null ) {
return ;
}
if ( root. getNo ( ) == no) {
root = null ;
return ;
} else {
root. delNode ( no) ;
}
}
public void threaded ( HeroNode node) {
if ( node == null ) {
return ;
}
threaded ( node. getLeft ( ) ) ;
if ( node. getLeft ( ) == null ) {
node. setLeft ( pre) ;
node. setLeftType ( 1 ) ;
}
if ( pre != null && pre. getRight ( ) == null ) {
pre. setRight ( node) ;
pre. setRightType ( 1 ) ;
}
pre = node;
threaded ( node. getRight ( ) ) ;
}
public void threaded ( ) {
this . threaded ( root) ;
}
public void infixOrder2 ( ) {
HeroNode node = root;
while ( node != null ) {
while ( node. getLeftType ( ) != 1 ) {
node = node. getLeft ( ) ;
}
System . out. println ( node) ;
while ( node. getRightType ( ) == 1 ) {
node = node. getRight ( ) ;
System . out. println ( node) ;
}
node = node. getRight ( ) ;
}
}
}
class HeroNode {
private int no;
private String name;
private HeroNode left;
private HeroNode right;
private int leftType;
private int rightType;
public int getLeftType ( ) {
return leftType;
}
public void setLeftType ( int leftType) {
this . leftType = leftType;
}
public int getRightType ( ) {
return rightType;
}
public void setRightType ( int rightType) {
this . rightType = rightType;
}
public HeroNode ( int no, String name) {
this . no = no;
this . name = name;
}
public int getNo ( ) {
return no;
}
public void setNo ( int no) {
this . no = no;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public HeroNode getLeft ( ) {
return left;
}
public void setLeft ( HeroNode left) {
this . left = left;
}
public HeroNode getRight ( ) {
return right;
}
public void setRight ( HeroNode right) {
this . right = right;
}
@Override
public String toString ( ) {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}' ;
}
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 no) {
if ( this . no == no) {
return this ;
}
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. preOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . right != null ) {
resNode = this . right. preOrderSearch ( no) ;
}
return resNode;
}
public HeroNode infixOrderSearch ( int no) {
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. infixOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . no == no) {
return this ;
}
if ( this . right != null ) {
resNode = this . right. infixOrderSearch ( no) ;
}
return resNode;
}
public HeroNode postOrderSearch ( int no) {
HeroNode resNode = null ;
if ( this . left != null ) {
resNode = this . left. postOrderSearch ( no) ;
}
if ( resNode != null ) {
return resNode;
}
if ( this . right != null ) {
resNode = this . right. postOrderSearch ( no) ;
}
if ( this . no == no) {
return this ;
}
return resNode;
}
public void delNode ( int no) {
if ( this . left != null && this . left. no == no) {
this . left = null ;
return ;
}
if ( this . right != null && this . right. no == no) {
this . right = null ;
return ;
}
if ( this . left != null ) {
this . left. delNode ( no) ;
}
if ( this . right != null ) {
this . right. delNode ( no) ;
}
}
}