/**
** 二叉树相关代码,只是基于一个大示例小步迭代,层层递进的增加功能代码.
** 这里只需要关注deleteNode()方法即可。
** 全贴出来,只是便于测试而已。
** 这里的删除节点,是限定条件的实现。
** 条件: 1.如果删除的节点是叶子节点,则删除该节点。
** 2.如果删除的节点非叶子节点,则删除该子树。
** 这里未考虑有子节点的节点,删除该节点之后,元素的位置 ** 关系。
*/
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( "后序搜索结束~~~~~~" );
binaryTree.deleteNote(3);
binaryTree.preOrder();
}
}
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);
}
public void deleteNote( int num ) {
if( node !=null ) {
if( node.getNum() == num ) {
node = null;
}else {
node.deleteNode(num);
}
}else {
System.out.println( "空树,无法删除~~~~~" );
}
}
}
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;
}
//删除节点
public void deleteNode( int num ) {
if( this.left != null && this.left.num == num ) {
this.left = null;
return;
}
if( this.right != null && this.right.num == num ) {
this.right = null;
return;
}
if( this.left !=null ) {
this.left.deleteNode(num);
}
if( this.right !=null ) {
this.right.deleteNode(num);
}
}
}
【java】【二叉树】【删除节点】
最新推荐文章于 2022-06-12 16:24:43 发布