二叉排序树的建立和先序遍历和层次遍历问题之java实现
2015-01-21 22:54
246 查看
import java.util.*;
class Node
{
int key;
Node left;
Node right;
Node(int key)
{
this.key = key;
left = null;
right = null;
}
}
class Tree
{
void insert(Node root,int k)
{
int value = root.key;
Node node = new Node(k);
if(k < value)
{
if(root.left == null)
{
root.left = node;
}
else
{
insert(root.left,k);
}
}
else if(k > value)
{
if(root.right == null)
root.right = node;
else
insert(root.right,k);
}
}
void mid(Node root)
{
if(root == null)
return;
else
{
mid(root.left); //中序遍历
System.out.print(root.key + " ");
mid(root.right);
}
}
void level(Node root) //层次遍历,注意用到了LinkedList,用来存储相关的节点,模拟一个队列的方式。
{
if(root == null)
return;
List<Node> queue = new LinkedList<Node>();
queue.add(root);
while(!(queue.isEmpty()))
{
Node temp = queue.remove(0); //用队列的方式来实现层次遍历,先进先出
System.out.print(temp.key + " ");
if(temp.left != null)
queue.add(temp.left);
if(temp.right != null)
queue.add(temp.right);
}
System.out.println();
}
}
public class Test
{
public static void main(String args[])
{
int array[] = {19,12,4,5,7};
Node root = new Node(array[0]);
Tree tree = new Tree();
for(int i = 0; i < array.length; i++)
{
tree.insert(root, array[i]);
}
tree.mid(root);
System.out.println();
tree.level(root);
}
}
class Node
{
int key;
Node left;
Node right;
Node(int key)
{
this.key = key;
left = null;
right = null;
}
}
class Tree
{
void insert(Node root,int k)
{
int value = root.key;
Node node = new Node(k);
if(k < value)
{
if(root.left == null)
{
root.left = node;
}
else
{
insert(root.left,k);
}
}
else if(k > value)
{
if(root.right == null)
root.right = node;
else
insert(root.right,k);
}
}
void mid(Node root)
{
if(root == null)
return;
else
{
mid(root.left); //中序遍历
System.out.print(root.key + " ");
mid(root.right);
}
}
void level(Node root) //层次遍历,注意用到了LinkedList,用来存储相关的节点,模拟一个队列的方式。
{
if(root == null)
return;
List<Node> queue = new LinkedList<Node>();
queue.add(root);
while(!(queue.isEmpty()))
{
Node temp = queue.remove(0); //用队列的方式来实现层次遍历,先进先出
System.out.print(temp.key + " ");
if(temp.left != null)
queue.add(temp.left);
if(temp.right != null)
queue.add(temp.right);
}
System.out.println();
}
}
public class Test
{
public static void main(String args[])
{
int array[] = {19,12,4,5,7};
Node root = new Node(array[0]);
Tree tree = new Tree();
for(int i = 0; i < array.length; i++)
{
tree.insert(root, array[i]);
}
tree.mid(root);
System.out.println();
tree.level(root);
}
}
相关文章推荐
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- java实现二叉树的建立,前中后序遍历,层次遍历,深度,节点个数等
- 数据结构--java实现二叉树的先序、中序、后序、层次遍历及根据先序中序建立二叉树
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- 关于java写二叉排序树的问题,实现各种节点的插入,删除,遍历
- 二叉树建立以及先序、中序、后序、层次遍历(JAVA 实现)
- java 二叉树的实现与先序遍历和层次遍历
- Java二叉树(一)--定义及前序、中序、后序、层次遍历及求高度的实现
- Java实现树的遍历(前序、中序、后续(递归|非递归)、层次)
- java语言实现二叉排序树(结点的增、删、查、改,递归、非递归遍历)
- java 实现二叉树的构建,先序,中序,后序,层次,递归,非递归的遍历
- 二叉树的建立&&前中后遍历(递归实现)&&层次遍历
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- JAVA语言实现二叉树的层次遍历的非递归算法及递归算法
- JAVA实现二叉树的遍历的非递归算法及递归算法(前序,中序,后序,层次)
- 二叉树的层次遍历----java实现
- java根据层次遍历建立二叉树
- 实现二叉树的先序遍历、中序遍历、后序遍历的递归非递归算法以及层次遍历算法
- java实现二叉树的建立及遍历方法
- java实现二叉树层次遍历