遍历二叉树
来源:互联网 发布:php exec函数用法 编辑:程序博客网 时间:2024/06/16 12:01
package d12遍历二叉树;//二叉树节点public class Node { //数据项 public long date; //数据项 public String sData; //左字节点 public Node leftChild; //右子节点 public Node rightChild; //构造方法 public Node(long value,String sData){ this.date=value; this.sData=sData; }}
package d12遍历二叉树;public class Tree { //根节点 public Node root; //插入节点 public void insert(long value,String aValue){ //封装节点 Node newNode =new Node(value,aValue); //引用当前节点 Node current = root; //引用父节点 Node parent; //如果root为null,也就是第一次插入的时候 if(root==null){ root=newNode; return; }else{ while(true){ //父节点指向当前节点 parent = current; //如果当前指向的节点数据比插入的要大,则向左走 if(current.date>value){ current = current.leftChild; if(current==null){ parent.leftChild=newNode; return; } }else{ current = current.rightChild; if(current==null){ parent.rightChild=newNode; return; } } } } } //查找结点 public Node find(long value){ //引用当前节点,从根节点开始 Node current = root; //循环,只要查找不等于当前的节点的数据项 while(current.date>value){ //进行比较,比较查找值和当前节点的大小 if(current.date>value){ current = current.leftChild; }else{ current=current.rightChild; }//如果查不到,返回为空 if(current==null){ return null; } } return current; } //删除结点 public void delete(){ } //前序遍历 public void frontOrder (Node localNode){ if(localNode!=null){ //访问根节点 System.out.println(localNode.date+","+localNode.sData); //前序遍历左子树 frontOrder(localNode.leftChild); //前序遍历右子树 frontOrder(localNode.rightChild); } } //中序遍历 public void inOrder(Node localNode){ if(localNode!=null){ //中序遍历左子树 inOrder(localNode.leftChild); //中序遍历根节点 System.out.println(localNode.date+","+localNode.sData); //中序遍历右子树 inOrder(localNode.rightChild); } }public void lastOrder(Node localNode){ if(localNode!=null){ //中序遍历左子树 inOrder(localNode.leftChild); //中序遍历右子树 inOrder(localNode.rightChild); //中序遍历根节点 System.out.println(localNode.date+","+localNode.sData); } }}
阅读全文
0 0
- 二叉树遍历、分层遍历
- 遍历二叉树--二叉树
- 【树】遍历二叉树
- 二叉树遍历
- 二叉树遍历
- 二叉树的遍历
- (原创)遍历二叉树
- 二叉树遍历-php
- 查找--遍历二叉树
- 遍历二叉树
- 二叉树遍历规则
- 二叉树的遍历
- 二叉树遍历
- 遍历二叉树
- 二叉树遍历问题
- 二叉树遍历
- 二叉树遍历(zz)
- 二叉树的遍历
- 二叉树的基本操作(插入、查找)
- class Introduction to Databases in Python
- java初级入门
- Android使用通知
- 在 Golang 中开发中使用 Makefile
- 遍历二叉树
- c-风格字符串和string类型的字符串的异同
- Spring Data JPA进阶-@Query注解
- Android studio中布局文件出现render problem问题
- 【CSS】学习笔记
- 二叉树操作(删除)
- hdu1232(基础并查集)
- 将两个字符串连接起来,不要用strcat
- 什么是POJO?