数据结构之树

在这里插入图片描述在这里插入图片描述
双亲节点是子树的根节点。
路径就是根节点到目标节点到路径。
层:
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

完全二叉树,从上往下,从左向右。
在这里插入图片描述
在这里插入图片描述
不连续就不是完全二叉树。
二叉树
在这里插入图片描述
定义一个节点代码如何:`

public class TreeNode {

    //权值
    int value;
    //左节点
    TreeNode lNode;
    //右节点
    TreeNode rNode;
    //构造方法
    public TreeNode(int value){
        //为权值设立值
        this.value=value;
    }
    //为左节点赋值
    public void setlNode(TreeNode lNode){
        this.lNode=lNode;
    }
    //为右节点赋值
    public void setrNode(TreeNode rNode){
        this.rNode=rNode;
    }
   //前序遍历
    public void frontshow() {
        //遍历前序遍历 根左右
        //左节点要前序遍历,
        System.out.println(value);
        if(lNode!=null){
            lNode.frontshow();
        }
        if(rNode!=null) {
            rNode.frontshow();
        }
    }
     //中序列遍历 左根右
    public void midshow() {
        //左边进行中序遍历
        if(lNode!=null) {
            lNode.midshow();
        }
        System.out.println(value);
        if(rNode!=null) {
            rNode.midshow();
        }
    }
    //后序遍历是左右根
    public void aftershow() {
        //
        if(lNode!=null){
            lNode.aftershow();
        }
        //
        if(rNode!=null){
            rNode.aftershow();
        }
        System.out.println(value);
    }
    
}

定义树

public class BinaryTree {

    TreeNode root;

    //为根节点赋值
    public void  setRoot(TreeNode root){
        this.root=root;
    }
    //获取根节点
    public TreeNode getRoot(){
        return  root;
    }

    public void frontshow() {
        root.frontshow();
    }

    public void midshow() {
        root.midshow();
    }

    public void aftershow() {
        root.aftershow();
    }
}

构造一棵树并且进行前中后序遍历

/**
 * 实现
 */
public class TestBinaryTree {
    public static void main(String[] args) {
       //构建一个树
        BinaryTree binaryTree=new BinaryTree();
        //创建一个节点
        TreeNode root=new TreeNode(1);
        //把根节点赋值给树
        binaryTree.setRoot(root);
        //创建一个左节点
        TreeNode rootL=new TreeNode(2);
        //把新创建的节点设置为root的左节点
        root.setlNode(rootL);
        //创建右节点
        TreeNode rootR=new TreeNode(3);
        //把新创建的节点设置成root节点的右节点
        root.setrNode(rootR);
        //为第二层的左节点创建两个子节点
        rootL.setlNode(new TreeNode(4));
        rootL.setrNode(new TreeNode(5));
        //为第二层的右节点创建两个子节点
        rootR.setlNode(new  TreeNode(6));
        rootR.setrNode(new TreeNode(7));
        //前序遍历树
      //  binaryTree.frontshow();
        //中序遍历树
        //binaryTree.midshow();
        //后序遍历树
        binaryTree.aftershow();
    }
}

补充其他两个功能就是对值进行查找–1.按值查找2.删除子树。
完整代码如何下:

/**
 * 定义一个树的节点--节点的权值,左节点,右节点
 */
public class TreeNode {

    //权值
    int value;
    //左节点
    TreeNode lNode;
    //右节点
    TreeNode rNode;
    //构造方法
    public TreeNode(int value){
        //为权值设立值
        this.value=value;
    }
    //为左节点赋值
    public void setlNode(TreeNode lNode){
        this.lNode=lNode;
    }
    //为右节点赋值
    public void setrNode(TreeNode rNode){
        this.rNode=rNode;
    }

    public void frontshow() {
        //遍历前序遍历 根左右
        //左节点要前序遍历,
        System.out.println(value);
        if(lNode!=null){
            lNode.frontshow();
        }
        if(rNode!=null) {
            rNode.frontshow();
        }
    }
     //中序列遍历 左根右
    public void midshow() {
        //左边进行中序遍历
        if(lNode!=null) {
            lNode.midshow();
        }
        System.out.println(value);
        if(rNode!=null) {
            rNode.midshow();
        }
    }
    //后序遍历是左右根
    public void aftershow() {
        //
        if(lNode!=null){
            lNode.aftershow();
        }
        //
        if(rNode!=null){
            rNode.aftershow();
        }
        System.out.println(value);
    }
     //前序查找
    public TreeNode search(int i) {
        TreeNode target=null;
        //对比当前节点
      if(this.value==i){
         return this;
         //当前节点的值不是要查找的节点
      }else{
          //查找左儿子
          if(lNode!=null){
              //右可能可以查到,也可能查不到
              target=lNode.search(i);
          }
          //不为空,说明找到了,
          if(target!=null){
              return target;
          }
          //查找右儿子
          if(rNode!=null){
              target=rNode.search(i);
          }
      }
      return target;
    }
    //删除子树
    public void delete(int i) {
        //链表是单项的
        TreeNode parent=this;
        //判断左儿子
        if(parent.lNode!=null &&parent.lNode.value==i){
            parent.lNode=null;
            return;
        }
        //判断右儿子
        if(parent.rNode!=null &&parent.rNode.value==i){
            parent.rNode=null;
            return;
        }
        //递归检查并且删除左儿子
        parent=lNode;
        if(parent!=null){
            parent.delete(i);
        }
        //递归检查并且删除右儿子
        parent=rNode;
        if(parent!=null){
            parent.delete(i);
        }
    }
}

二叉树

package tree;

/**
 * 构建一树。树有根节点,左子树,右子树..--链式存储的二叉树
 */
public class BinaryTree {

    TreeNode root;

    //为根节点赋值
    public void  setRoot(TreeNode root){
        this.root=root;
    }
    //获取根节点
    public TreeNode getRoot(){
        return  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 search(int i) {

            return root.search(i);

    }

    public void delete(int i) {
        //如果删除的刚刚好是主节点
      if(root.value==i){
          root=null;
      }else{
          root.delete(i);
      }
    }
}

完整测试代码

package tree;

/**
 * 实现
 */
public class TestBinaryTree {
    public static void main(String[] args) {
       //构建一个树
        BinaryTree binaryTree=new BinaryTree();
        //创建一个节点
        TreeNode root=new TreeNode(1);
        //把根节点赋值给树
        binaryTree.setRoot(root);
        //创建一个左节点
        TreeNode rootL=new TreeNode(2);
        //把新创建的节点设置为root的左节点
        root.setlNode(rootL);
        //创建右节点
        TreeNode rootR=new TreeNode(3);
        //把新创建的节点设置成root节点的右节点
        root.setrNode(rootR);
        //为第二层的左节点创建两个子节点
        rootL.setlNode(new TreeNode(4));
        rootL.setrNode(new TreeNode(5));
        //为第二层的右节点创建两个子节点
        rootR.setlNode(new  TreeNode(6));
        rootR.setrNode(new TreeNode(7));

        //前序遍历树
      //  binaryTree.frontshow();
        //中序遍历树
        //binaryTree.midshow();
        //后序遍历树
        //binaryTree.aftershow();
        //
        //前序查找
        TreeNode result=binaryTree.search(2);

        System.out.println(result);
        //删除子树
        binaryTree.delete(2);
        binaryTree.frontshow();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大数据学习爱好者

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值