Repository navigation
Expand file tree
/
Copy pathBTree.java
More file actions
118 lines (103 loc) · 2.33 KB
/
Copy pathBTree.java
File metadata and controls
118 lines (103 loc) · 2.33 KB
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
114
115
116
117
118
public class BTree<T>
{
private TreeNode<T> head;
public BTree(T head)
{
this.head = new TreeNode<T>(head);
}
public BTree(TreeNode<T> head)
{
this.head = head;
}
public void setLR(T headE, T leftE, T rightE)
{
TreeNode<T> h = head.get(headE);
if (h == null)
throw new NullPointerException("Parent node does not exist!");
TreeNode<T> lc = new TreeNode<T>(leftE);
lc.setParent(h);
h.setLeftChild(lc);
TreeNode<T> rc = new TreeNode<T>(rightE);
rc.setParent(h);
h.setRightChild(rc);
}
public void setL(T headE, T leftE)
{
TreeNode<T> h = head.get(headE);
if (h == null)
throw new NullPointerException("Parent node does not exist!");
TreeNode<T> lc = new TreeNode<T>(leftE);
lc.setParent(h);
h.setLeftChild(lc);
}
public void setR(T headE, T rightE)
{
TreeNode<T> h = head.get(headE);
if (h == null)
throw new NullPointerException("Parent node does not exist!");
TreeNode<T> rc = new TreeNode<T>(rightE);
rc.setParent(h);
h.setRightChild(rc);
}
public void insertL(T headE, T leftE)
{
TreeNode<T> h = head.get(headE);
if (h == null)
throw new NullPointerException("Parent node does not exist!");
TreeNode<T> lc = new TreeNode<T>(leftE);
TreeNode<T> plc = h.getLeftChild();
if(plc == null)
throw new NullPointerException("Parent node does not have a left child!");
lc.setParent(h);
lc.setLeftChild(plc);
h.setLeftChild(lc);
}
public void insertR(T headE, T rightE)
{
TreeNode<T> h = head.get(headE);
if (h == null)
throw new NullPointerException("Parent node does not exist!");
TreeNode<T> rc = new TreeNode<T>(rightE);
TreeNode<T> prc = h.getRightChild();
if(prc == null)
throw new NullPointerException("Parent node does not have a left child!");
rc.setParent(h);
rc.setRightChild(prc);
h.setRightChild(rc);
}
public void deleteNode(T search)
{
head.deleteNode(search);
}
public TreeNode<T> get(T headE)
{
return head.get(headE);
}
public int size()
{
return head.size();
}
public int depth()
{
return head.depth();
}
public void prettyPrintTree()
{
BTreePrinter.printNode(head);
}
public void printPreOrder()
{
head.printPreOrder();
System.out.println();
}
public void printPostOrder()
{
head.printPostOrder();
System.out.println();
}
public void printInOrder()
{
head.printInOrder();
System.out.println();
}
}