大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
成都创新互联是一家专业提供玉树企业网站建设,专注与网站建设、网站制作、H5建站、小程序制作等业务。10年已为玉树众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。
import javax.swing.*;
class TreeDemo extends JFrame
{
public TreeDemo()
{
setSize(400,300);
setTitle("演示怎样使用JTree");
show();
JScrollPane jPanel=new JScrollPane();
getContentPane().add(jPanel);
JTree jtree=new JTree();
jPanel.getViewport().add(jtree,null);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Example5_25
{
public static void main(String[] args)
{
TreeDemo frame=new TreeDemo();
}
}
其中JScrollPane是一个带滚动条的面板类。
将对象加入到带滚动条的面板类中,在将已建的数放入到其中。
就可建立一个系统默认的树结构。
public class Tree {
private int treeId;
private String treeType;// 树种类型
private int count; //种植数量
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getTreeId() {
return treeId;
}
public void setTreeId(int treeId) {
this.treeId = treeId;
}
public String getTreeType() {
return treeType;
}
public void setTreeType(String treeType) {
this.treeType = treeType;
}
}
public class Address {
private int addCode;//地区编码
private String area;//地名
public int getAddCode() {
return addCode;
}
public void setAddCode(int addCode) {
this.addCode = addCode;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
import java.util.HashMap;
import java.util.Map;
public class People {
private int userId;
private String username;
private MapString,MapString,Integer map;
/**
* 传入地区和树种,种树成功。保存到map中。
* @param address
* @param tree
*/
public void plantingTrees(String address,Tree tree){
Map map = new HashMap();
map.put(tree.getTreeType(),tree.getCount());
this.map.put(address,map);
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public MapString, MapString, Integer getMap() {
return map;
}
public void setMap(MapString, MapString, Integer map) {
this.map = map;
}
}
二叉树的相关操作,包括创建,中序、先序、后序(递归和非递归),其中重点的是java在先序创建二叉树和后序非递归遍历的的实现。
package com.algorithm.tree;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
public class Tree {
private Node root;
public Tree() {
}
public Tree(Node root) {
this.root = root;
}
//创建二叉树
public void buildTree() {
Scanner scn = null;
try {
scn = new Scanner(new File("input.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
root = createTree(root,scn);
}
//先序遍历创建二叉树
private Node createTree(Node node,Scanner scn) {
String temp = scn.next();
if (temp.trim().equals("#")) {
return null;
} else {
node = new Node((T)temp);
node.setLeft(createTree(node.getLeft(), scn));
node.setRight(createTree(node.getRight(), scn));
return node;
}
}
//中序遍历(递归)
public void inOrderTraverse() {
inOrderTraverse(root);
}
public void inOrderTraverse(Node node) {
if (node != null) {
inOrderTraverse(node.getLeft());
System.out.println(node.getValue());
inOrderTraverse(node.getRight());
}
}
//中序遍历(非递归)
public void nrInOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
System.out.println(node.getValue());
node = node.getRight();
}
}
//先序遍历(递归)
public void preOrderTraverse() {
preOrderTraverse(root);
}
public void preOrderTraverse(Node node) {
if (node != null) {
System.out.println(node.getValue());
preOrderTraverse(node.getLeft());
preOrderTraverse(node.getRight());
}
}
//先序遍历(非递归)
public void nrPreOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
while (node != null || !stack.isEmpty()) {
while (node != null) {
System.out.println(node.getValue());
stack.push(node);
node = node.getLeft();
}
node = stack.pop();
node = node.getRight();
}
}
//后序遍历(递归)
public void postOrderTraverse() {
postOrderTraverse(root);
}
public void postOrderTraverse(Node node) {
if (node != null) {
postOrderTraverse(node.getLeft());
postOrderTraverse(node.getRight());
System.out.println(node.getValue());
}
}
//后续遍历(非递归)
public void nrPostOrderTraverse() {
StackNode stack = new StackNode();
Node node = root;
Node preNode = null;//表示最近一次访问的节点
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
node = node.getLeft();
}
node = stack.peek();
if (node.getRight() == null || node.getRight() == preNode) {
System.out.println(node.getValue());
node = stack.pop();
preNode = node;
node = null;
} else {
node = node.getRight();
}
}
}
//按层次遍历
public void levelTraverse() {
levelTraverse(root);
}
public void levelTraverse(Node node) {
QueueNode queue = new LinkedBlockingQueueNode();
queue.add(node);
while (!queue.isEmpty()) {
Node temp = queue.poll();
if (temp != null) {
System.out.println(temp.getValue());
queue.add(temp.getLeft());
queue.add(temp.getRight());
}
}
}
}
//树的节点
class Node {
private Node left;
private Node right;
private T value;
public Node() {
}
public Node(Node left,Node right,T value) {
this.left = left;
this.right = right;
this.value = value;
}
public Node(T value) {
this(null,null,value);
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}
测试代码:
package com.algorithm.tree;
public class TreeTest {
/**
* @param args
*/
public static void main(String[] args) {
Tree tree = new Tree();
tree.buildTree();
System.out.println("中序遍历");
tree.inOrderTraverse();
tree.nrInOrderTraverse();
System.out.println("后续遍历");
//tree.nrPostOrderTraverse();
tree.postOrderTraverse();
tree.nrPostOrderTraverse();
System.out.println("先序遍历");
tree.preOrderTraverse();
tree.nrPreOrderTraverse();
//
}
}
package tree;
import java.util.LinkedList;
import java.util.List;
/**
* 功能:把一个数组的值存入二叉树中,然后进行3种方式的遍历
*
* 参考资料0:数据结构(C语言版)严蔚敏
*
* 参考资料1:
*
* 参考资料2:
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
public class BinTreeTraverse2 {
private int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
private static ListNode nodeList = null;
/**
* 内部类:节点
*
* @author ocaicai@yeah.net @date: 2011-5-17
*
*/
private static class Node {
Node leftChild;
Node rightChild;
int data;
Node(int newData) {
leftChild = null;
rightChild = null;
data = newData;
}
}
public void createBinTree() {
nodeList = new LinkedListNode();
// 将一个数组的值依次转换为Node节点
for (int nodeIndex = 0; nodeIndex array.length; nodeIndex++) {
nodeList.add(new Node(array[nodeIndex]));
}
// 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树
for (int parentIndex = 0; parentIndex array.length / 2 - 1; parentIndex++) {
// 左孩子
nodeList.get(parentIndex).leftChild = nodeList
.get(parentIndex * 2 + 1);
// 右孩子
nodeList.get(parentIndex).rightChild = nodeList
.get(parentIndex * 2 + 2);
}
// 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理
int lastParentIndex = array.length / 2 - 1;
// 左孩子
nodeList.get(lastParentIndex).leftChild = nodeList
.get(lastParentIndex * 2 + 1);
// 右孩子,如果数组的长度为奇数才建立右孩子
if (array.length % 2 == 1) {
nodeList.get(lastParentIndex).rightChild = nodeList
.get(lastParentIndex * 2 + 2);
}
}
/**
* 先序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void preOrderTraverse(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
preOrderTraverse(node.leftChild);
preOrderTraverse(node.rightChild);
}
/**
* 中序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void inOrderTraverse(Node node) {
if (node == null)
return;
inOrderTraverse(node.leftChild);
System.out.print(node.data + " ");
inOrderTraverse(node.rightChild);
}
/**
* 后序遍历
*
* 这三种不同的遍历结构都是一样的,只是先后顺序不一样而已
*
* @param node
* 遍历的节点
*/
public static void postOrderTraverse(Node node) {
if (node == null)
return;
postOrderTraverse(node.leftChild);
postOrderTraverse(node.rightChild);
System.out.print(node.data + " ");
}
public static void main(String[] args) {
BinTreeTraverse2 binTree = new BinTreeTraverse2();
binTree.createBinTree();
// nodeList中第0个索引处的值即为根节点
Node root = nodeList.get(0);
System.out.println("先序遍历:");
preOrderTraverse(root);
System.out.println();
System.out.println("中序遍历:");
inOrderTraverse(root);
System.out.println();
System.out.println("后序遍历:");
postOrderTraverse(root);
}
}
import java.util.Stack;//导入栈包
public class newtree {
private newtree lchild;// 声明数据成员
private newtree rchild;
private char data;
private newtree root;
public newtree(newtree l, newtree r, char data) {// 有参构造函数进行成员赋值
lchild = l;
rchild = r;
this.data = data;
}
public newtree() {// 无参构造函数创建树
newtree f = new newtree(null, null, 'f');
newtree g = new newtree(null, null, 'g');
newtree d = new newtree(null, null, 'd');
newtree e = new newtree(null, null, 'e');
newtree b = new newtree(d, e, 'b');
newtree c = new newtree(f, g, 'c');
newtree a = new newtree(b, c, 'a');
this.root=a;
}
public void visit(newtree p) {/* 输出数据 */
System.out.print(p.data);// 访问结点
}
@SuppressWarnings("unchecked")
public void InOrder() {/* 输入数据 */
newtree p=this.root;//你建了一棵树要把根节点赋值进去啊
Stack s = new Stack();
while (p != null || !s.isEmpty()) /* 处理数据:进行中序遍历 */
{
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = (newtree) s.pop();
p.visit(p);//this指的是当前的类对象
p = p.rchild;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder();
}
}
//根据你的代码改了一个
import java.util.Stack;//导入栈包
public class newtree {
public Tree createTree() {// 无参构造函数创建树
Tree f = new Tree(null, null, 'f');
Tree g = new Tree(null, null, 'g');
Tree d = new Tree(null, null, 'd');
Tree e = new Tree(null, null, 'e');
Tree b = new Tree(d, e, 'b');
Tree c = new Tree(f, g, 'c');
Tree a = new Tree(b, c, 'a');
return a;
}
public void InOrder(Tree p) {/* 输入数据 */
StackTree s = new StackTree();
while (p != null || !s.isEmpty()) { /* 处理数据:进行中序遍历 */
if (p != null) {
s.push(p);
p = p.lchild;
} else {
p = s.pop();
System.out.print(p.data);
p = p.rchild;
}
}
}
public void inOrder1(Tree p) {
if (p == null)
return;
inOrder1(p.lchild);
System.out.print(p.data);
inOrder1(p.rchild);
}
public static void main(String[] args) {
newtree h = new newtree();// 声明变量,变量赋值
h.InOrder(h.createTree());
System.out.println();
h.inOrder1(h.createTree());
}
}
class Tree {
Tree lchild;// 声明数据成员
Tree rchild;
char data;
Tree(Tree lchild, Tree rchild, char data) {
this.lchild = lchild;
this.rchild = rchild;
this.data = data;
}
}