原帖地址:http://topic.csdn.net/u/20090218/17/6f07b6db-5019-4102-bc72-cc4db3eae890.html
1)要求 <!-- google_ad_section_start -->用java写二叉树算法,实现添加数据形成二叉树功能,并以先序的方式打印出来:
定义Node
public class Node {
public int key;
Node right;
Node left;
public Node(int key) {
this.key = key;
}
public String toString() {
return "my key is " + key;
}
}
定义二叉树
public class Tree {
public Node root;
public Node findNode(Node node) {
Node current = root;
while (current.key != node.key) {
if (current.key > node.key) {
current = current.left;
} else {
current = current.right;
}
if (current == null) {
return null;
}
}
return current;
}
public void insertNode(Node node) {
if (root == null) {
root = node;
} else {
Node current = root;
Node parent;
while (true) {
parent = current;
if (current.key > node.key) {
current = current.left;
if (current == null) {
parent.left = node;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = node;
return;
}
}
}
}
}
public void inOrder(Node current) {
if (current != null) {
inOrder(current.left);
System.out.println(current);
inOrder(current.right);
}
}
}
分享到:
相关推荐
java二叉树实现 (简单实现,入门用) /**创建二叉树*/ public BinaryTree createTree(String treeStr); /**寻找结点*/ public BinaryTree findNode(BinaryTree tree ,char sign); /**找所给结点的左子树*/ ...
二叉树的java实现
二叉树是每个结点最多有两个子树的有序树。java 二叉树新增删除,遍历二叉树
java实现二叉树非递归前序中序后序遍历
简单的描述了JAVA实现二叉树
一个简单的课程设计,使用Java来实现二叉树的中序遍历
用java写的二叉树,一种特别的二叉树,右子树大于左子树,具体的名称不记得了。
java实现二叉树遍历demo,一个简单是实例
java二叉树查找树生成删除添加节点
二叉树可视化Java语言实现,包含四个字文件、BinaryNode、Show1_12、Display_Tree、TreeControl。直接可以运行,方便快捷。
Java的二叉树实现。 解压后恢复到Eclipse工作区中即可。
java实现 二叉树的遍历 前序遍历用到递归, 中序和后序遍历用到栈, 其实还是有一定难度的
java实现的二叉树源码,包括建立、前序、中序、后序遍历算法,查找算法
java用队列实现的二叉树程序//入队 public void enqueue(E e); //出队 public E dequeue(); //取队列第一个 public E front(); //队列是否为空 public boolean isEmpty(); //队列大小 public int size...
Java实现二叉树的基本操作
java使用jtree动态实现二叉树,包含二叉树的插入删除很查找
java语言实现的中国电信超级号码簿跳转功能 数据结构为二叉树 运用孩子兄弟链表法把一棵树变成了二叉树 实现了跳转功能
java实现二叉树数据结构,简单明了,免费下载
代码实现: 二叉树的查找、插入、删除和输出根节点到当前节点的路径 二叉树的前序遍历,中序遍历和后续遍历 TreeNode.java --定义树节点 Mytree.java----创建树结构和上述功能函数 TestTree.java --测试上述的功能
资源是txt版本,整合了思路及程序。内容仅供参考,不可用于商业,否则后果自负。