二叉树的创建和三种遍历方式
来源:互联网 发布:中国知网cnki数据库 编辑:程序博客网 时间:2024/06/06 00:41
public class BinaryTree {
public static void main(String[] args)
{
int[] arr = {2,4,6,8,22,34,77,44};
BinaryTree bt = new BinaryTree();
for(int i=0;i<arr.length;i++)
{
bt.buildTree(bt.root, arr[i]);
}
bt.preOrder(bt.root);
bt.inOrder(bt.root);
bt.postOrder(bt.root);
}
private static Node root;
private class Node{
private Node left;
private Node right;
private int data;
public Node(int data)
{
this.data = data;
}
}
public BinaryTree()
{
this.root = null;
}
public static void buildTree(Node node,int data)
{
if(root==null)
{
root = new Node(data);
}
else
{
if(data<node.data)
{
if(node.left==null)
node.left = new Node(data);
else
buildTree(node.left,data);
}
else if(data>node.data)
{
if(node.right==null)
node.right = new Node(data);
else
buildTree(node.right,data);
}
}
}
public static void preOrder(Node node)//前序遍历
{
if(node!=null)
{
System.out.println(node.data);
preOrder(node.left);
preOrder(node.right);
}
}
public static void inOrder(Node node)//中序遍历
{
if(node!=null)
{
inOrder(node.left);
System.out.println(node.data);
inOrder(node.right);
}
}
public static void postOrder(Node node)//后序遍历
{
if(node!=null)
{
postOrder(node.left);
postOrder(node.right);
System.out.println(node.data);
}
}
}
public static void main(String[] args)
{
int[] arr = {2,4,6,8,22,34,77,44};
BinaryTree bt = new BinaryTree();
for(int i=0;i<arr.length;i++)
{
bt.buildTree(bt.root, arr[i]);
}
bt.preOrder(bt.root);
bt.inOrder(bt.root);
bt.postOrder(bt.root);
}
private static Node root;
private class Node{
private Node left;
private Node right;
private int data;
public Node(int data)
{
this.data = data;
}
}
public BinaryTree()
{
this.root = null;
}
public static void buildTree(Node node,int data)
{
if(root==null)
{
root = new Node(data);
}
else
{
if(data<node.data)
{
if(node.left==null)
node.left = new Node(data);
else
buildTree(node.left,data);
}
else if(data>node.data)
{
if(node.right==null)
node.right = new Node(data);
else
buildTree(node.right,data);
}
}
}
public static void preOrder(Node node)//前序遍历
{
if(node!=null)
{
System.out.println(node.data);
preOrder(node.left);
preOrder(node.right);
}
}
public static void inOrder(Node node)//中序遍历
{
if(node!=null)
{
inOrder(node.left);
System.out.println(node.data);
inOrder(node.right);
}
}
public static void postOrder(Node node)//后序遍历
{
if(node!=null)
{
postOrder(node.left);
postOrder(node.right);
System.out.println(node.data);
}
}
}
0 0
- 二叉树的创建和三种遍历方式
- 二叉树的创建和三种遍历方式
- C预言的二叉树的创建和三种遍历方式
- 二叉树的java构建和三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式(递归、非递归和Morris遍历)
- 三种创建二叉树的方式
- 二叉树的三种遍历方式的循环和递归的实现方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树的创建及遍历方式
- 二叉树的创建与三种形式的遍历
- 二叉树的三种遍历方式(递归和非递归的实现方法)
- 二叉树的三种遍历方式的递归实现和非递归实现
- 二叉树的递归和非递归方式的三种遍历
- 【调试】Core Dump是什么?Linux下如何正确永久开启?
- (java)Majority Element
- hdu2032
- ajaxfileupload.js异步上传图片
- 微信公众号开发---微信请求服务端取值问题
- 二叉树的创建和三种遍历方式
- POJ---3468-A Simple Problem with Integers(线段树)
- 【调试】Linux下超强内存检测工具Valgrind
- 编程思维的点滴
- map端执行流程分析
- 屏幕旋转概述
- maven-surefire-customresult插件
- n个数逆序
- CSDN Markdown简明教程1-关于Markdown