java二叉树家谱实现

java二叉树家谱实现,第1张

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

}

}

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

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

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

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

}

Java,是由Sun Microsystems公司程序设计语言和Java平台的总称。用Java实现的HotJava浏览器(支持Java applet)显示了Java的魅力:跨平台、动态的Web、Internet计算。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11823837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存