首页 > 其他分享 >树结构及前中后续遍历

树结构及前中后续遍历

时间:2024-01-24 17:57:43浏览次数:26  
标签:right 树结构 Tree 及前 遍历 null root public left

public class Tree {
    public static void main(String[] args) {
        Tree root = new Tree(50);
        Tree.insert(root, 30);
        Tree.insert(root, 60);
        Tree.insert(root, 70);
        Tree.insert(root, 100);
        Tree.insert(root, 80);
        Tree.VLR(root);
        System.out.println();
        Tree.LDR(root);
        System.out.println();
        Tree.LRD(root);
    }
    
    
    /**数元素*/
    int item;
    /**左节点*/
    Tree left;
    /**右节点*/
    Tree right;

    /**构造函数*/
    public Tree(int e) {
        this.item = e;
        this.left = null;
        this.right = null;
    }

    /**获取元素*/
    public int getItem() {
        return item;
    }

    /**插入元素*/
    public static void insert(Tree root, int value) {
        if(root.item < value) {
            if (root.right == null) {
                root.right = new Tree(value);
            } else {
                insert(root.right, value);
            }
        } else {
            if (root.left == null) {
                root.left = new Tree(value);
            } else {
                insert(root.left, value);
            }
        }
    }

    /**前序排序-VLR*/
    public static void VLR(Tree root) {
        print(root);
        if (root.left != null) {
            VLR(root.left);
        }
        if (root.right != null) {
            VLR(root.right);
        }
    }
   /**中序排序-LDR*/
   public static void LDR(Tree root) {
       if (root.left != null) {
           LDR(root.left);
       }
       print(root);
       if (root.right != null) {
           LDR(root.right);
       }
   }

   /**后序排序-LRD*/
   public static void LRD(Tree root) {
       if (root.left != null) {
           LRD(root.left);
       }
       if (root.right != null) {
           LRD(root.right);
       }
       print(root);
   }

    /**打印*/
    private static void print(Tree root) {
        System.out.print(root.getItem() + " ");
    }

}

  

标签:right,树结构,Tree,及前,遍历,null,root,public,left
From: https://www.cnblogs.com/Small-sunshine/p/17985389

相关文章

  • 遍历删除集合元素
    1publicclassTest{2publicstaticvoidmain(String[]args){3List<String>list=newArrayList<>();4list.add("张三");5list.add("张三");6list.add("李四");7......
  • 图的遍历
    链式前向星存图点击查看代码#include<bits/stdc++.h>usingnamespacestd;inth[100005],nx[100005],t[100005],cnt;intans[100005];intread1(){ charcc=getchar(); while(!(cc>=48&&cc<=57)) { if(cc=='-') { break; } cc=getch......
  • 遍历二叉树非递归实现
    实现1.前序遍历publicvoidpreOrderNor(TreeNoderoot){if(root==null){return;}Stack<TreeNode>stack=newStack<>();stack.push(root);while(!stack.isEmpty()){TreeNodecur......
  • Java中遍历方法对比
    DemopublicclassTest{publicstaticvoidmain(String[]args){test(10);test(100);test(1000);test(10000);}publicstaticvoidtest(intsize){//1.组装数组List<String>list=list(siz......
  • 遍历二叉树
    二叉树前言二叉树的遍历主要有深度优先遍历和广度优先遍历,深度优先遍历是优先访问一个子树上的所有节点,访问的属性是竖向的,而广度优先遍历则是优先访问同一层的所有节点,访问属性是横向的。深度优先遍历深度优先遍历主要有三种顺序:前序遍历——根左右中序遍历——左根......
  • 动态规划(4) 完全背包的遍历顺序
    目录377组合总和Ⅳ进阶版爬楼梯322零钱兑换377组合总和ⅣclassSolution{public:intcombinationSum4(vector<int>&nums,inttarget){intn=nums.size();vector<int>dp(target+1,0);dp[0]=1;for(intj=0;j<=target;j++)......
  • 二叉树结构与递归实现前中后序遍历
    1.二叉树结构2.二叉树节点遍历顺序前序:每颗子树以中—》左—》右遍历ABDEHCFG 中序:每颗子树以左 —》中—》右遍历DBEHAFCG 后序:每颗子树以左 —》右—》中遍历DHEBFGCA 代码实现:publicclassBinaryTree{stat......
  • 数组遍历的方法
    1、forEach()forEach() 方法对数组的每个元素执行一次给定的函数,不会改变原数组,没有返回值。数组中的每个值都会调用回调函数,回调函数有三个参数:currentValue:必需。当前元素。index:可选。当前元素的索引值。arr:可选。当前元素所属的数组对象。//forEach不会改......
  • 遍历链表,将节点接到末端 【1月16日学习笔记】
    点击查看代码//遍历链表,将节点接到末端#include<iostream>usingnamespacestd;structnode{ intdata;//数据 node*next;//尾巴};//定义节点结构体node*A;//头指针固定,globalvariabl......
  • 性能篇:List集合遍历元素用哪种方式更快?
    嗨大家好,我是小米!今天给大家分享一篇关于Java集合框架性能的文章,话题是:“如果让你使用for循环以及迭代循环遍历一个ArrayList,你会使用哪种方式呢?原因是什么?LinkedList呢?”废话不多说,让我们直入主题!ArrayList的get元素源码介绍ArrayList,作为Java集合框架中的一个重要类,是基于数组......