运用Java数据结构的知识创建树,内容是族谱。

Python021

运用Java数据结构的知识创建树,内容是族谱。,第1张

每一个节点有一个成员变量引用下一个节点就行了。

大致实现了一下单向链表 没有加入异常也没有仔细考虑实现的代码的效率,可以参考下。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

   

public class LinkListTest {

    public static void main(String[] args) {

        LinkList<String> ll=new LinkList<String>()

        ll.add("a")

        ll.add("b")

        ll.add("c")

        ll.add("d")

        ll.remove(1)

        System.out.println(ll.get(0))

        System.out.println(ll.get(1))

        System.out.println(ll.get(2))

        System.out.println(ll.get(3))

        System.out.println(ll.size())

    }

}

class LinkList<T>{

    private Node<T> frist=null

    private Node<T> last=null

    private int size=0

                                                                          

    public void add(T t){

        if(frist==null){

            Node<T> node=new Node<T>()

            node.setT(t)

            size++

            frist=node

            last=node

        }else{        

            Node<T> node=new Node<T>()       

            node.setT(t)

            last.setNextNode(node)

            size++

            last=node

        }

    }

                                                                          

    public T get(int i){      

        if(i>=0&&i<size){

            Node<T> nod=null

            for(int n=0n<=in++){

                if(n==0)

                   nod=frist

                else

                   nod=nod.getNextNode()             

                if(i==n){

                    return nod.getT()

                }         

            }         

        }

         return null

    } 

                                                                          

    public void remove(int i){

        if(i>=0&&i<size){

           if(size<2){

               frist=null

               last=null

               size=0

           }else{

               size--

              if(i==0){

                 frist=frist.getNextNode()        

              }else{      

                    Node<T> nod1=null

                    Node<T> nod2=null

                    for(int n=0n<=in++){

                        if(n==0){

                           nod1=frist

                           nod2=frist

                        }else{

                           nod2=nod1

                           nod1=nod1.getNextNode()                      

                        } 

                        if(i==n){

                            if(nod1!=null)

                                nod2.setNextNode(nod1.getNextNode())

                            else{

                                nod2.setNextNode(null)

                                last=nod2

                            }

                        }             

                    } 

              }   

            }

        }

    }

                                                                          

                                                                          

                                                                          

    public int size(){

        return size

    }

}

class Node <T>{

    public T getT() {

        return t

    }

    public void setT(T t) {

        this.t = t

    }

    public Node<T> getNextNode() {

        return nextNode

    }

    public void setNextNode(Node<T> nextNode) {

        this.nextNode = nextNode

    }

    private T t

    private Node<T> nextNode=null

}

mport java.awt.BorderLayout

import java.awt.Dimension

import java.awt.event.ActionEvent

import java.awt.event.ActionListener

import java.util.Random

import javax.swing.JButton

import javax.swing.JFrame

import javax.swing.JPanel

import javax.swing.JScrollPane

import javax.swing.JTree

import javax.swing.tree.DefaultMutableTreeNode

public class Randomtree extends JFrame {

private JTree tree

public static String[] school = { "初中课程", "高中课程", "大学课程" }

public static String[] color = { "颜色", "运动", "食物" }

public static String[] plant = { "植物", "动物", "人" }

public static String[][] school2= {

{ "初中一年级", "初中二年级", "初中三年级"}, {"高中一年级", "高中二年级",

"高中三年级"}, {"大学一年级", "大学二年级", "大学三年级", "大学四年级"} }

public static String[][] color2 = {

{ "绿色", "白色", "红色"}, {"足球", "篮球",

"羽毛球"}, {"面包", "牛奶", "披萨", "热狗"} }

public static String[][] plant2 = {

{ "玫瑰花", "月季花", "海棠花"}, {"猪", "狗",

"猫"}, {"黄种人", "黑种人", "白种人", } }

public static void main(String[] args) {

// TODO 自动生成方法存根

new Randomtree()

}

public Randomtree() {

super()

final Random random=new Random()

setVisible(true)

setSize(300,400)

tree = new JTree()

final JPanel panel = new JPanel()

panel.setPreferredSize(new Dimension(0, 40))

getContentPane().add(panel, BorderLayout.NORTH)

final JScrollPane scrollPane = new JScrollPane()

scrollPane.setPreferredSize(new Dimension(300, 350))

getContentPane().add(scrollPane, BorderLayout.CENTER)

final JButton button = new JButton()

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

int k=random.nextInt(3)

tree=getTree(k)

scrollPane.setViewportView(tree)

}

})

scrollPane.setViewportView(null)

button.setText("随机生成树")

panel.add(button)

pack()

}

protected JTree getTree(int n) {

String[] second=null

String[][] three=null

if(n==0){second=schoolthree=school2}

if(n==1){second=colorthree=color2}

if(n==2){second=plantthree=plant2}

DefaultMutableTreeNode root=new DefaultMutableTreeNode("root")

for(int i=0i<second.lengthi++){

DefaultMutableTreeNode secondNode=new DefaultMutableTreeNode(second[i])

for (int j=0j<three[i].lengthj++){

DefaultMutableTreeNode threetNode=new DefaultMutableTreeNode(three[i][j])

secondNode.add(threetNode)

}

root.add(secondNode)

}

JTree tree=new JTree(root)

tree.expandRow(1)

tree.expandRow(5)

tree.expandRow(9)

return tree

}

}

简单的 例子你可以模仿一下

tree 英 [tri:]   美 [tri]  :n. 树木料树状图宗谱;vt. 把?赶上树使处于困境把鞋型插入(鞋内)

短语

family tree 家族树 家谱 家庭树 族谱

Suffix Tree [计] 后缀树 后缀树实现 字尾树

tree hyrax 树蹄兔属 树蹄兔

Leftist tree 左偏树 左倾树

Tree sitting 树坐 国际常见的树坐

Tree spiking 树钉

Metric tree 度量树

Fenwick tree 树状数组

camphor tree [林] 樟树 [林] 樟脑树 香樟树 香樟

扩展资料

双语例句

1、You are absolutely correct. The leaves are from a bay tree

你说得很对,这是月桂树的叶子。

2、The peach tree is wormy.

桃树长虫了。

3、He dug a hole in our yard on Edgerton Avenue to plant a maple tree when I was born.

我出生的时候,他在埃杰顿大街我们家的园圃里挖了个坑,种了棵枫树。

4、China has the world's most ancient tree species--metasequoia.

中国有世界最古老的树种--水杉。

5、A vandal with a chainsaw cut down a tree.

一个故意破坏公物的人用链锯伐倒了一棵树。