无规则二叉树
封装的挺好的感觉这种写法
前序遍历:
- 根左右
中序遍历:
- 左根右
后序遍历:
- 左右根
前序、中序、后序查找:
- 类似遍历
二叉树
class BinaryTree{
private HeroNode root;
public BinaryTree(HeroNode root) {
this.root = root;
}
/**
* 前序遍历
*/
public void preOrder(){
if(this.root != null){
this.root.preOrder();
}else {
System.out.println("二叉树为空");
}
}
// 中序遍历
public void infixOrder(){
if(this.root != null){
this.root.infixOrder();
}else {
System.out.println("二叉树为空");
}
}
// 后序遍历
public void postOrder(){
if(this.root != null){
this.root.postOrder();
}else {
System.out.println("二叉树为空");
}
}
// 前序查找
public HeroNode preOrderSearch(int id){
if(this.root != null){
return this.root.preOrderSearch(id);
} else {
return null;
}
}
// 中序查找
public HeroNode infixOrderSearch(int id){
if(this.root != null){
return this.root.infixOrderSearch(id);
}else {
return null;
}
}
// 后序查找
public HeroNode postOrderSearch(int id){
if(this.root != null){
return this.root.postOrderSearch(id);
}else {
return null;
}
}
// 节点删除
public void delNode(int id){
if(this.root != null){
if(this.root.id == id){
this.root = null;
}else{
// 递归删除
this.root.delNode(id);
}
}else {
System.out.println("二叉树为空");
return ;
}
}
}
@Test
public void testBinaryTree(){
// 创建一颗二叉树
}
HeroNode 节点
/**
* 节点
*/
class HeroNode {
public Integer id;
public String name;
public HeroNode left;
public HeroNode right;
public HeroNode() {
}
public HeroNode(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"id=" + id +
", 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 id 需要查找的值
* @return
*/
public HeroNode preOrderSearch(int id){
// 根节点
if(this.id == id){
return this;
}
HeroNode temp = null;
// 左子节点
if(this.left != null){
temp = this.left.preOrderSearch(id);
}
// 如果不为空,即找到了
if(temp != null){
return temp;
}
// 右子节点
if(this.right != null){
temp = this.right.preOrderSearch(id);
}
// 未找到返回为 null, 找到了也返回找到的 temp 节点
return temp;
}
/**
* 中序遍历查找
* @param id
* @return
*/
public HeroNode infixOrderSearch(int id){
HeroNode temp = null;
if(this.left != null){
temp = this.left.infixOrderSearch(id);
}
if(temp != null){
return temp;
}
// 根节点
if(this.id == id){
return this;
}
// 左节点
if(this.right != null){
temp = this.right.infixOrderSearch(id);
}
// 未找到为 null, 找到即不为 null
return temp;
}
/**
* 后序遍历查找
* @param id
* @return
*/
public HeroNode postOrderSearch(int id){
HeroNode temp = null;
if(this.left != null){
temp = this.left.postOrderSearch(id);
}
if(temp != null){
return temp;
}
// 右节点
if(this.right != null){
temp = this.right.postOrderSearch(id);
}
if(this.id == id){
return this;
}
return temp;
}
/**
* 删除节点
* @param id
*/
public void delNode(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.delNode(id);
}
// 向右子树递归删除
if(this.right != null){
this.right.delNode(id);
}
}
}