美文网首页
算法_二叉树

算法_二叉树

作者: X亡口月贝凡 | 来源:发表于2019-06-14 16:59 被阅读0次

package com.demo.calculate;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.demo.calculate.bean.BinaryTree;
import com.demo.calculate.bean.TreeNode;

public class TreeActivit extends Activity {
private BinaryTree binaryTree;
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tree);
//创建一棵空树
binaryTree = new BinaryTree();
TreeNode root = new TreeNode(1);
binaryTree.setRoot(root);
//根节点的左节点
TreeNode leftNode = new TreeNode(2);
leftNode.setNodeLeft(new TreeNode(4));
leftNode.setNodeRight(new TreeNode(5));
//根节点的右节点
TreeNode rightNode = new TreeNode(3);
rightNode.setNodeLeft(new TreeNode(6));
rightNode.setNodeRight(new TreeNode(7));
root.setNodeLeft(leftNode);
root.setNodeRight(rightNode);


1.png 2.png 3.png 4.png

package com.demo.calculate.bean;

public class BinaryTree {
private int value;
private TreeNode root;
public int getValue() {
return value;
}

public void setValue(int value) {
    this.value = value;
}

public TreeNode getRoot() {
    return root;
}

public void setRoot(TreeNode root) {
    this.root = root;
}

public void frontShow() {
    if (root!=null){
        root.frontShow();
    }
}

public void midShow() {
    if (root!=null){
        root.midShow();
    }

}

public void afterShow() {
    if (root!=null){
        root.afterShow();
    }
}

public TreeNode frontSearch(int i) {
    if (root!=null){
        return root.frontSearch(i);
    }
    return  null;
}

public void deleteNode(int i) {
    if (root!=null){
        root.deleteNode(i);
    }
}

}
package com.demo.calculate.bean;

import android.util.Log;

public class TreeNode {
private int value;
private TreeNode nodeLeft;
private TreeNode nodeRight;
public void setNodeLeft(TreeNode nodeLeft) {
this.nodeLeft = nodeLeft;
}
public void setNodeRight(TreeNode nodeRight) {
this.nodeRight = nodeRight;
}
public TreeNode(int value) {
this.value = value;
}
public void frontShow() {
Log.i("tag",value+"");
if (nodeLeft!=null){
nodeLeft.frontShow();
}
if (nodeRight!=null){
nodeRight.frontShow();
}
}
public int getValue() {
return value;
}

public void setValue(int value) {
    this.value = value;
}

public void midShow() {
    if (nodeLeft!=null){
        nodeLeft.midShow();
    }
    Log.i("tag",value+"");
    if (nodeRight!=null){
        nodeRight.midShow();
    }
}

public void afterShow() {
    if (nodeLeft!=null){
        nodeLeft.afterShow();
    }
    if (nodeRight!=null){
        nodeRight.afterShow();
    }
    Log.i("tag",value+"");
}
//前序查找
public TreeNode frontSearch(int i) {
    TreeNode target=null;
    if (this.value ==i){
        return this;
    }else{
        if (nodeLeft!=null){
            target = nodeLeft.frontSearch(i);
        }
        if (target!=null){
            return target;
        }
        if (nodeRight!=null){
            target = nodeRight.frontSearch(i);
        }
    }
    return target;
}

public void deleteNode(int i) {
    TreeNode parent = this;
    if (this.value==i){
        parent = null;
    }
    //判断左儿子
    if (parent!=null && parent.nodeLeft!=null && parent.nodeLeft.value==i){
        parent.nodeLeft = null;
        return;
    }
    //判断右儿子
    if (parent!=null && parent.nodeRight!=null && parent.nodeRight.value==i){
        parent.nodeRight = null;
        return;
    }
    //递归删除左面的儿子
    parent = nodeLeft;
    if (parent!=null){
        parent.deleteNode(i);
    }
    //递归删除右面的儿子
    parent = nodeRight;
    if (parent!=null){
        parent.deleteNode(i);
    }
}

}

相关文章

  • 每日Leetcode—算法(10)

    100.相同的树 算法: 101.对称二叉树 算法: 104.二叉树的最大深度 算法: 107.二叉树的层次遍历 ...

  • 每日Leetcode—算法(11)

    110.平衡二叉树 算法: 111.二叉树的最小树深 算法: 112.路径总和 算法:

  • Algorithm小白入门 -- 二叉树

    二叉树二叉树构造二叉树寻找重复子树 1. 二叉树 基本二叉树节点如下: 很多经典算法,比如回溯、动态规划、分治算法...

  • 二叉树的基本算法

    二叉树的基本算法 树、二叉树 的基本概念,参考数据结构算法之美-23讲二叉树基础(上):树、二叉树[https:/...

  • 二叉树的中序遍历(Java)——Morris迭代算法

    二叉树的中序遍历 对于此题而言,若采用递归算法(简单),我们使用深度优先算法来遍历二叉树。若采用迭代算法,我们使用...

  • 2019-10-22

    最优二叉树搜索算法。

  • 翻转二叉树(Java)

    翻转二叉树 对于此题而言,我们使用深度优先算法来遍历二叉树。 1、深度优先算法是根据二叉树的路径进行遍历2、广度优...

  • 二叉树遍历(递归算法和非递归算法)

    实验三 二叉树遍历(递归算法和非递归算法) 一.实验目的 1.掌握二叉树的存储结构与基本操作 2.掌握二叉树的遍历...

  • LeetCode基础算法-树

    LeetCode基础算法-树 LeetCode 树 基础算法 1. 二叉树的最大深度 给定一个二叉树,找出其最大深...

  • 【算法题】递归求二叉树深度

    二叉树的深度算法,是二叉树中比较基础的算法了。对应 LeetCode 第104题。 然后你会发现 LeetCode...

网友评论

      本文标题:算法_二叉树

      本文链接:https://www.haomeiwen.com/subject/jovpfctx.html