package com company public class Tree class Node Student data Node lef

 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
package com.company;
public class Tree {
class Node {
Student data;
Node left;
Node right;
Node(Student d){
data = d;
}
}
Node root;
public void Insert(Student s){
Node n = new Node (s);
_Insert(root,n);
}
private Node _Insert(Node current, Node new_elem){
if(current == null) {
root = new_elem;
return new_elem;
}
if(new_elem.data.Name.compareTo( current.data.Name) != 1){//????????????????????????????????????
current.left = _Insert(current.left,new_elem);
current = Rotation_R(current);
}else {
current.right = _Insert(current.right,new_elem);
current = Rotation_L(current);
}
return current;
}
public Node Rotation_R(Node current){
Node tmp = current.left;
current.left = tmp.right;
tmp.right = current;
current = tmp;
return current;
}
public Node Rotation_L(Node current){
Node tmp = current.right;
current.right = tmp.left;
tmp.left = current;
current = tmp;
return current;
}
public void PrintTree(){
PrintElementTree(this.root, 0);
}
private void PrintElementTree(Node current ,int level){
if(current != null){
for (int i = 0;i<level;i++){
System.out.print("\t");
}
System.out.println(current.data);
level++;
PrintElementTree(current.left,level);
PrintElementTree(current.right,level);
}
}
public void PrintByName(String name){
PrintElementTreeByName(this.root,name);
}
private void PrintElementTreeByName(Node current,String name){
if(current != null){
PrintElementTreeByName(current.left,name);
PrintElementTreeByName(current.right,name);
if(current.data.Name.equals(name)){
System.out.print(current.data+"\n");
}
}
}
}