-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
124 lines (104 loc) · 3.24 KB
/
BinaryTree.java
File metadata and controls
124 lines (104 loc) · 3.24 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
119
120
121
122
123
124
/**
* Implements binary trees.
*
* @author Nicholas R. Howe
* @version CSC 212, November 2021
*/
public class BinaryTree<E> {
/** The value at this node */
private E data;
/** Left child of this node */
private BinaryTree<E> left;
/** Right child of this node */
private BinaryTree<E> right;
/** This constructor creates a leaf node */
public BinaryTree(E data) {
this.data = data;
right = left = null;
}
/** This constructor creates a branch node */
public BinaryTree(E data, BinaryTree<E> left, BinaryTree<E> right) {
this.data = data;
this.right = right;
this.left = left;
}
/** This constructor creates a deep copy of the entire tree structure */
public BinaryTree(BinaryTree<E> tree) {
this.data = tree.data;
this.left = (tree.left == null)? null:(new BinaryTree<E>(tree.left));
this.right = (tree.right == null)? null:(new BinaryTree<E>(tree.right));
}
/** Accessor for node data */
public E getData() {
return data;
}
/** Accessor for left child */
public BinaryTree<E> getLeft() {
return left;
}
/** Accessor for right child */
public BinaryTree<E> getRight() {
return right;
}
/** Manipulator for node data */
public void setData(E data) {
this.data = data;
}
/** Manipulator for left child */
public void setLeft(BinaryTree<E> left) {
this.left = left;
}
/** Manipulator for right child */
public void setRight(BinaryTree<E> right) {
this.right = right;
}
/** Determines whether a tree is empty */
public boolean isEmpty(BinaryTree<E> node) {
return (node == null);
}
/** Determines whether this tree is a leaf */
public boolean isLeaf() {
return (left == null)&&(right == null);
}
/** Determines whether this tree is a branch */
public boolean isBranch() {
return (left != null)||(right != null);
}
/** Counts the number of nodes */
public int count() {
int lcount = (left == null) ? 0 : left.count();
int rcount = (right == null) ? 0 : right.count();
return lcount+rcount+1;
}
/** Computes the height of the tree */
public int height() {
int lheight = (left == null) ? 0 : left.height();
int rheight = (right == null) ? 0 : right.height();
return (lheight > rheight) ? lheight+1 : rheight+1;
}
/** Creates a string representation */
public String toString() {
return inorderString(this);
}
public static <T> String preorderString(BinaryTree<T> t) {
if (t==null) {
return "";
} else {
return "("+t.data+" "+preorderString(t.left)+" "+preorderString(t.right)+")";
}
}
public static <T> String inorderString(BinaryTree<T> t) {
if (t==null) {
return "";
} else {
return "("+inorderString(t.left)+" "+t.data+" "+inorderString(t.right)+")";
}
}
public static <T> String postorderString(BinaryTree<T> t) {
if (t==null) {
return "";
} else {
return "("+postorderString(t.left)+" "+postorderString(t.right)+" "+t.data+")";
}
}
}