whycxzp
2021-03-04 6195e70334232c589f12b6fc0b091cf0be7cc583
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
110
111
112
113
package com.whyc.dto.binaryTree;
 
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
 
/**
 * 二叉树
 */
public class BinaryTree {
 
    TreeNode root =null;
 
    public BinaryTree() {
        this.root = new TreeNode(0,"A");
    }
 
    public BinaryTree(TreeNode root) {
        this.root = root;
    }
 
    @Override
    public String toString() {
        return "BinaryTree{" +
                "root=" + root +
                '}';
    }
 
    /**
     * 创建二叉树,手动传统
     */
    public BinaryTree createBinaryTree(){
        TreeNode nodeB = new TreeNode(2, "B");
        TreeNode nodeC = new TreeNode(3, "C");
        TreeNode nodeD = new TreeNode(4, "D");
        TreeNode nodeE = new TreeNode(5, "E");
        TreeNode nodeF = new TreeNode(6, "F");
 
        root.leftChild = nodeB;
        root.rightChild = nodeC;
 
        nodeB.leftChild = nodeD;
        nodeB.rightChild =nodeF;
 
        BinaryTree binaryTree = new BinaryTree(root);
        return binaryTree;
 
    }
 
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
        Class<BinaryTree> binaryTreeClass = BinaryTree.class;
        Method method = binaryTreeClass.getDeclaredMethod("createBinaryTree");
        BinaryTree root = (BinaryTree) method.invoke(binaryTreeClass.newInstance());
 
 
 
    }
 
    class TreeNode{
        private int index;
        private String data;
        private TreeNode leftChild;
        private TreeNode rightChild;
 
        public TreeNode(int index, String data) {
            this.index = index;
            this.data = data;
        }
 
        public int getIndex() {
            return index;
        }
 
        public void setIndex(int index) {
            this.index = index;
        }
 
        public String getData() {
            return data;
        }
 
        public void setData(String data) {
            this.data = data;
        }
 
        public TreeNode getLeftChild() {
            return leftChild;
        }
 
        public void setLeftChild(TreeNode leftChild) {
            this.leftChild = leftChild;
        }
 
        public TreeNode getRightChild() {
            return rightChild;
        }
 
        public void setRightChild(TreeNode rightChild) {
            this.rightChild = rightChild;
        }
 
        @Override
        public String toString() {
            return "TreeNode{" +
                    "index=" + index +
                    ", data='" + data + '\'' +
                    ", leftChild=" + leftChild +
                    ", rightChild=" + rightChild +
                    '}';
        }
    }
 
 
}