package com.study.tree;
/**
* 前序\中序\后序查找主要关注的还是顺序。
* 什么顺序先检查 根节点(node)视为具体 前 - 中 - 后序的查找算法得因由。
* 所以,只要有了 顺序 的思路,就不难理解。
* 唯一需要关注的是,前、中、后序的算法效率。
* 目前测试的结果是:后序最优。
* 但碍于目前理解尚浅,不懂的还很多,只好残念留之。
**/
public class BinaryTreeDemo1 {
public static void main(String[] args) {
BinaryTree1 binaryTree = new BinaryTree1();
HeroNode1 root= new HeroNode1(1, "宋江");
HeroNode1 node2 = new HeroNode1(2, "吴用");
HeroNode1 node3 = new HeroNode1(3, "卢俊义");
HeroNode1 node4 = new HeroNode1(4, "林冲");
HeroNode1 node5 = new HeroNode1(5, "关胜");
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node5);
node3.setRight(node4);
binaryTree.setRoot(root);
System.out.println(binaryTree.preSearch(5));
System.out.println( "前序搜索结束~~~~~~" );
System.out.println( binaryTree.infixSearch(5) );
System.out.println( "中序搜索结束~~~~~~" );
System.out.println( binaryTree.postSearch(5) );
System.out.println( "后序搜索结束~~~~~~" );
}
}
class BinaryTree1{
private HeroNode1 node;
public void setRoot( HeroNode1 root ) {
this.node = root;
}
public void preOrder() {
if( node != null ) {
this.node.preOrder();
}else{
System.out.println( "二叉树为空,无法遍历" );
}
}
public void infixOrder() {
if( node !=null ) {
this.node.infixOrder();
}else{
System.out.println( "二叉树为空,无法遍历" );
}
}
public void postOrder() {
if( node !=null ) {
this.node.postOrder();
}else{
System.out.println( "二叉树为空,无法遍历" );
}
}
public HeroNode1 preSearch(int num) {
return this.node.preSearch(num);
}
public HeroNode1 infixSearch( int num ) {
return this.node.infixSearch(num);
}
public HeroNode1 postSearch( int num ) {
return this.node.postSearch(num);
}
}
class HeroNode1{
private int num;
private String name;
private HeroNode1 left;
private HeroNode1 right;
public HeroNode1(int num, String name) {
this.num = num;
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HeroNode1 getLeft() {
return left;
}
public void setLeft(HeroNode1 left) {
this.left = left;
}
public HeroNode1 getRight() {
return right;
}
public void setRight(HeroNode1 right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode1 [num=" + num + ", 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 );
}
/**
* 前序搜索
* @param num
* @return
*/
public HeroNode1 preSearch( int num ) {
System.out.println( "前序搜索~~~~~~~~" );
if(this.num == num) {
return this;
}
HeroNode1 resNode = null;
if( this.left !=null ) {
resNode = this.left.preSearch(num);
}
if(resNode !=null) {
return resNode;
}
if( this.right !=null ) {
resNode = this.right.preSearch(num);
}
return resNode;
}
/**
* 中序搜索
* @param num
* @return
*/
public HeroNode1 infixSearch( int num ) {
HeroNode1 resultNode = null;
if( this.left !=null ) {
resultNode = this.left.infixSearch(num);
}
if( resultNode != null ) {
return resultNode;
}
System.out.println( "中序搜索开始~~~~" );
if( this.num == num ) {
return this;
}
if( this.right != null ) {
resultNode = this.right.infixSearch(num);
}
return resultNode;
}
/**
* 后序搜索
* @param num
* @return
*/
public HeroNode1 postSearch( int num ) {
HeroNode1 resultNode = null;
if( this.left != null ) {
resultNode = this.left.postSearch(num);
}
if( resultNode != null ) {
return resultNode;
}
if( this.right !=null ) {
resultNode = this.right.postSearch(num);
}
if( resultNode != null ) {
return resultNode;
}
System.out.println( "后序搜索开始~~~~" );
if( this.num == num ) {
return this;
}
return resultNode;
}
}
【java】【二叉树】【前序、中序、后序查找】
最新推荐文章于 2021-11-24 16:07:50 发布