关于javatree实现的信息
本篇文章给大家谈谈javatree实现,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java(算法与数据结构)tree
代码实现[一]部分
package ChapterEight;
class Tree {
class Node {
public long value;
public Node leftChild;
public Node rightChild;
public Node(long value) {
this.value = value;
leftChild = null;
rightChild = null;
}
}
public Node root;
public Tree() {
root = null;
}
// 向树中插入一个节点
public void insert(long value) {
Node newNode = new Node(value);
// 树是空的
if (root == null)
root = newNode;
else {
Node current = root;
Node parentNode;
while (true) {
parentNode = current;
if (value current.value) {
current = current.leftChild;
// 要插入的节点为左孩子节点
if (current == null) {
parentNode.leftChild = newNode;
return;
}
} else {
// 要插入的节点为右孩子节点
current = current.rightChild;
if (current == null) {
parentNode.rightChild = newNode;
return;
}
}
}
}
}
// 先续遍历树中的所有节点
public void preOrder(Node currentRoot) {
if (currentRoot != null) {
System.out.print(currentRoot.value + " ");
preOrder(currentRoot.leftChild);
preOrder(currentRoot.rightChild);
}
}
// 中续遍历树中的所有节点
public void inOrder(Node currentNode) {
if (currentNode != null) {
inOrder(currentNode.leftChild);
System.out.print(currentNode.value + " ");
inOrder(currentNode.rightChild);
}
}
// 后续遍历树中的所有节点
public void postOrder(Node currentNode) {
if (currentNode != null) {
postOrder(currentNode.leftChild);
postOrder(currentNode.rightChild);
System.out.print(currentNode.value + " ");
}
}
public void traverse(int traverseType) {
switch (traverseType) {
case 1:
preOrder(root);
break;
case 2:
inOrder(root);
break;
case 3:
postOrder(root);
break;
default:
break;
}
// 依据树节点的值删除树中的一个节点
public boolean delete(int value) {
// 遍历树过程中的当前节点
Node current = root;
// 要删除节点的父节点
Node parent = root;
// 记录树的节点为左孩子节点或右孩子节点
boolean isLeftChild = true;
while (current.value != value) {
parent = current;
// 要删除的节点在当前节点的左子树里
if (value current.value) {
isLeftChild = true;
current = current.leftChild;
}
// 要删除的节点在当前节点的右子树里
else {
isLeftChild = false;
current = current.rightChild;
}
// 在树中没有找到要删除的节点
if (current == null)
return false;
}
// 要删除的节点为叶子节点
if (current.leftChild == null current.rightChild == null) {
// 要删除的节点为根节点
if (current == root)
root = null;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = null;
// 要删除的节点为右孩子节点
else
parent.rightChild = null;
}
// 要删除的节点有左孩子节点,没有右孩子节点
else if (current.rightChild == null) {
// 要删除的节点为根节点
if (current == null)
root = current.leftChild;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = current.leftChild;
// 要删除的节点为右孩子节点
else
parent.rightChild = current.leftChild;
}
// 要删除的节点没有左孩子节点,有右孩子节点
else if (current.leftChild == null) {
// 要删除的节点为根节点
if (current == root)
root = root.rightChild;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = current.rightChild;
// 要删除的节点为右孩子节点
else
parent.rightChild = current.rightChild;
}
// 要删除的接节点既有左孩子节点又有右孩子节点
else {
Node successor = getSuccessor(current);
// 要删除的节点为根节点
if (current == root)
root = successor;
// 要删除的节点为左孩子节点
else if (isLeftChild)
parent.leftChild = successor;
// 要删除的节点为右孩子节点
else
parent.rightChild = successor;
}
return true;
}
// 找到要删除节点的替补节点
private Node getSuccessor(Node delNode) {
// 替补节点的父节点
Node successorParent = delNode;
// 删除节点的替补节点
Node successor = delNode;
Node current = delNode.rightChild;
while (current != null) {
// successorParent指向当前节点的上一个节点
successorParent = successor;
// successor变为当前节点
successor = current;
current = current.leftChild;
}
// 替补节点的右孩子节点不为空
if (successor != delNode.rightChild) {
successorParent.leftChild = successor.rightChild;
successor.rightChild = delNode.rightChild;
}
return successor;
}
}
public class TreeApp {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(8);
tree.insert(50);
tree.insert(45);
tree.insert(21);
tree.insert(32);
tree.insert(18);
tree.insert(37);
tree.insert(64);
tree.insert(88);
tree.insert(5);
tree.insert(4);
tree.insert(7);
System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();
System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();
System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();
System.out.println(tree.delete(7));
System.out.print("PreOrder : ");
tree.traverse(1);
System.out.println();
System.out.print("InOrder : ");
tree.traverse(2);
System.out.println();
System.out.print("PostOrder : ");
tree.traverse(3);
System.out.println();
}
}
如何用Java实现树形结构?
[java] view plain copy
package com.tree.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args){
showTree();
}
public static void showTree(){
Connection conn=null;
ResultSet rs = null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost/tree?user=rootpassword=root");
/*stmt=conn.createStatement();
rs=stmt.executeQuery("select * from country where pid=0");
while(rs.next()){
System.out.println(rs.getString("actile"));*/
tree(conn,0,0);
// }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
if(conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void tree(Connection conn,int id,int level){
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
String sql = "select * from country where pid = " + id;
rs = stmt.executeQuery(sql);
while(rs.next()) {
StringBuffer strPre = new StringBuffer("");
for(int i=0; ilevel; i++) {
strPre.append(" ");
}
System.out.println(strPre + rs.getString("actile"));
if(rs.getInt("is_leaf") != 0)
tree(conn, rs.getInt("id"), level + 1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}
if(stmt != null) {
stmt.close();
stmt = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据库
[sql] view plain copy
create database tree;
use tree;
create table country
(
id int primary key auto_increment,
pid int,
actile varchar(40),
is_leaf int
);
insert into country values(1,0, '中国',1);
insert into country values(2,1,'北京',0);
insert into country values(3,0,'美国',1);
insert into country values(4,3,'纽约',0);
insert into country values(5,1,'浙江',1);
insert into country values(6,5,'杭州',1);
insert into country values(7,6,'滨江',0);
java TreeSet 倒序是怎么实现的?
TreeSet是一个有序的集合。
第一:构造、增加、遍历、删除和判断是否包含某个元素同HashSet是一致的。、
第二:证明TreeSet是一个有序的集合。
TreeSet hashSet = new TreeSet();
hashSet.add("a"); //向集合中添加一个字符串
hashSet.add("e");
hashSet.add("b");
hashSet.add("d");
hashSet.add("c");
Iterator it = hashSet.iterator();
while(it.hasNext()){
System.out.println(it.next()+",");
}
输出结果是:
a,
b,
c,
d,
e,
注意:(1)从结果中可以看出元素被排序了,但是这个用默认的排序方法。如何自定义排序呢?可以实现Comparator接口来自定义排序。例如:
import java.util.Comparator;
import ws.wph.android.util.StringUtils;
public class MyCmp implements Comparator {
public int compare(Object element1, Object element2) {
int x = element2.toString().compareTo(element1.toString());
return x;
}
}
然后将该类的对象作为TreeSet构造方法的实参,即TreeSet hashSet = new TreeSet(new
MyCmp());。原理是:向TreeSet增加元素时,自动调用MyCmp类的compare(Object element1, Object
element2)方法,根据方法返回值决定element1和element2的顺序。(此时的输出结果是:e,
d,
c,
b,
a,)
(2)当element1 == element2时返回0,element1 element2 返回正数,element1 element2返回负数。
第三:按照学生成绩排序,当成绩相同时按照学号排序
public int compare(Object element1, Object element2) {
int x=0;
Stuendt s1 = (Stuendt)element1;
Stuendt s2 = (Stuendt)element2;
if(s1.getScore() s2.getScore()){
x=-1;
}else if(s1.getScore() s2.getScore()){
x=1;
}else{
x = s1.getSno().compareTo(s2.getSno());
}
return x;
}
(3)将汉字转换成拼音
public static String getPingYin(String src){
char[] t1 = null;
t1=src.toCharArray();
String[] t2 = new String[t1.length];
HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
t3.setVCharType(HanyuPinyinVCharType.WITH_V);
String t4="";
int t0=t1.length;
try {
for (int i=0;it0;i++)
{
//判断是否为汉字字符
if(java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+"))
{
t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
t4+=t2[0];
}
else
t4+=java.lang.Character.toString(t1[i]).toLowerCase();
}
return t4;
}catch (BadHanyuPinyinOutputFormatCombination e1) {
e1.printStackTrace();
}
return t4;
}
但是需要导入一个包
Java怎么实现输出是一个tree结构
树节点类:
package cn.com.tree;
public class Node {
private Integer id;
private Integer parentId;
private String name;
private String link;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
}
输出树形菜单类:
package cn.com.tree;
import java.util.ArrayList;
import java.util.List;
public class Tree {
private StringBuffer html = new StringBuffer();
private ListNode nodes;
public Tree(ListNode nodes){
this.nodes = nodes;
}
public String buildTree(){
html.append("ul");
for (Node node : nodes) {
Integer id = node.getId();
if (node.getParentId() == null) {
html.append("\r\nli id='" + id + "'" + node.getName()+ "/li");
build(node);
}
}
html.append("\r\n/ul");
return html.toString();
}
private void build(Node node){
ListNode children = getChildren(node);
if (!children.isEmpty()) {
html.append("\r\nul");
for (Node child : children) {
Integer id = child.getId();
html.append("\r\nli id='" + id + "'" + child.getName()+ "/li");
build(child);
}
html.append("\r\n/ul");
}
}
private ListNode getChildren(Node node){
ListNode children = new ArrayListNode();
Integer id = node.getId();
for (Node child : nodes) {
if (id.equals(child.getParentId())) {
children.add(child);
}
}
return children;
}
}
测试类:
package zzj.test;
import java.util.ArrayList;
import java.util.List;
import cn.com.tree.Node;
import cn.com.tree.Tree;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ListNode nodes = new ArrayListNode();
Node node1 = new Node();
node1.setId(1);
node1.setName("node1");
node1.setParentId(null);
node1.setLink(null);
nodes.add(node1);
Node node11 = new Node();
node11.setId(11);
node11.setName("node11");
node11.setParentId(1);
node11.setLink(null);
nodes.add(node11);
Node node111 = new Node();
node111.setId(111);
node111.setName("node111");
node111.setParentId(11);
node111.setLink(null);
nodes.add(node111);
Node node12 = new Node();
node12.setId(12);
node12.setName("node12");
node12.setParentId(1);
node12.setLink(null);
nodes.add(node12);
Node node2 = new Node();
node2.setId(2);
node2.setName("node2");
node2.setParentId(null);
node2.setLink(null);
nodes.add(node2);
Node node21 = new Node();
node21.setId(21);
node21.setName("node21");
node21.setParentId(2);
node21.setLink(null);
nodes.add(node21);
Node node3 = new Node();
node3.setId(3);
node3.setName("node3");
node3.setParentId(null);
node3.setLink(null);
nodes.add(node3);
Tree tree = new Tree(nodes);
System.out.println(tree.buildTree());
}
}
javatree实现的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javatree实现的信息别忘了在本站进行查找喔。
发布于:2022-11-21,除非注明,否则均为
原创文章,转载请注明出处。