java写二叉排序树的插入、查找和遍历
来源:互联网 发布:数据库sql语句增删改查 编辑:程序博客网 时间:2024/06/05 07:49
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)
{
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();
}
void find(Node root,int k)
{
int value = root.key;
if(value == k)
{
System.out.println("Yes!");
}
else if(value != k)
{
if(value > k)
{
if(root.left != null)
find(root.left,k);
else
System.out.println("No!");
}
else if(value < k)
{
if(root.right != null)
find(root.right,k);
else
System.out.println("No!");
}
}
}
}
public class Test
{
public static void main(String args[])
{
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in); //注意:这里使用了scanner类,用来从键盘输入
int n = scan.nextInt();
int array[] = new int[n];
for(int j = 0; j < n; j++)
array[j] = scan.nextInt();
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);
System.out.print("请输入要查询的数字:");
int k = scan.nextInt();
tree.find(root, k);
}
}
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)
{
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();
}
void find(Node root,int k)
{
int value = root.key;
if(value == k)
{
System.out.println("Yes!");
}
else if(value != k)
{
if(value > k)
{
if(root.left != null)
find(root.left,k);
else
System.out.println("No!");
}
else if(value < k)
{
if(root.right != null)
find(root.right,k);
else
System.out.println("No!");
}
}
}
}
public class Test
{
public static void main(String args[])
{
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in); //注意:这里使用了scanner类,用来从键盘输入
int n = scan.nextInt();
int array[] = new int[n];
for(int j = 0; j < n; j++)
array[j] = scan.nextInt();
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);
System.out.print("请输入要查询的数字:");
int k = scan.nextInt();
tree.find(root, k);
}
}
0 0
- java写二叉排序树的插入、查找和遍历
- Java实现二叉排序树的插入、查找、删除
- 二叉排序树的插入、遍历和删除
- 二叉排序树的查找、插入和删除
- 二叉排序树的查找、插入和删除
- 二叉排序树的查找、插入
- 关于java写二叉排序树的问题,实现各种节点的插入,删除,遍历
- 二叉排序树的插入、查找、删除、遍历(C语言版)
- 二叉排序树的相关操作(插入,查找,删除,遍历等)
- 【数据结构树表的查找】二叉排序树详解和代码(生成、插入、查找、最大值、最小值、删除、中序遍历、销毁)
- 二叉排序树 插入 遍历 查找 (V0.2)
- 二叉排序树的创建,插入,遍历
- 二叉排序树的查找、删除、插入
- 二叉排序树的插入,查找,删除
- 二叉排序树的查找-插入-删除
- 二叉排序树的建立和遍历(java)
- java实现二叉排序树的建立,查找,插入,删除
- 二叉排序树的创建、插入和遍历(代码)
- MySQL的btree索引和hash索引的区别
- C#操作WORD公用类
- 红帽集群套件RHCS四部曲(概念篇)
- FMDB使用
- 音视频编解码学习笔记(四)--转载
- java写二叉排序树的插入、查找和遍历
- Linux ubuntu中取消文件夹或文件等右下解一把锁的标志的方法
- Memcached调试参数&常用命令
- c#获取常见路径
- JS操作select大全
- Mybatis参数设置
- POJ 1321 棋盘问题(kuangbin带你飞 专题一)
- unity3d 如何拦截射线碰撞界面的消息
- git 学习使用教程