-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeNode.java
More file actions
139 lines (115 loc) · 3.35 KB
/
BinaryTreeNode.java
File metadata and controls
139 lines (115 loc) · 3.35 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.util.ArrayList;
/**
* A simple interface for binary trees. An empty binary tree is
* represented with the value null; a non-empty tree by its root
* node.
*/
public interface BinaryTreeNode<E> {
/**
* Returns the data stored in this node.
*/
public E getData();
/**
* Modifies the data stored in this node.
*/
public void setData(E data);
/**
* Returns the ancestor of this node that has no parent,
* or returns this node if it is the root.
*/
public BinaryTreeNode<E> getRoot();
/**
* Returns the parent of this node, or null if this node is a root.
*/
public BinaryTreeNode<E> getParent();
/**
* Sets the parent node of the current node.
*/
public void setParent(BinaryTreeNode<E> parent);
/**
* Returns the left child of this node, or null if it does
* not have one.
*/
public BinaryTreeNode<E> getLeft();
/**
* Removes child from its current parent and inserts it as the
* left child of this node. If this node already has a left
* child it is removed.
*/
public void setLeft(BinaryTreeNode<E> child);
/**
* Returns the right child of this node, or null if it does
* not have one.
*/
public BinaryTreeNode<E> getRight();
/**
* Removes child from its current parent and inserts it as the
* right child of this node. If this node already has a right
* child it is removed.
*/
public void setRight(BinaryTreeNode<E> child);
/**
* Returns true if the node has any children.
* Otherwise, returns false.
*/
public boolean isParent( );
/**
* Returns true if the node is childless.
* Otherwise, returns false.
*/
public boolean isLeaf( );
/**
* Returns true if the node has a left child
*/
public boolean hasLeftChild( );
/**
* Returns true if the node has a right child
*/
public boolean hasRightChild( );
/**
* Returns the number of edges in the path from the root to this node.
*/
public int getDepth( );
/**
* Returns the number of edges in the path from the root
* to the node with the maximum depth.
*/
public int getHeight( );
/**
* Returns the number of nodes in the subtree rooted at this node.
*/
public int size( );
/**
* Removes this node, and all its descendants, from whatever
* tree it is in. Does nothing if this node is a root.
*/
public void removeFromParent();
/**
* Returns the path from this node to the specified descendant.
* If no path exists, returns an empty list.
*/
public ArrayList<BinaryTreeNode<E>> pathTo( BinaryTreeNode<E> descendant );
/**
* Returns the path to this node from the specified ancestor.
* If no path exists, returns an empty list.
*/
public ArrayList<BinaryTreeNode<E>> pathFrom( BinaryTreeNode<E> ancestor );
/**
* Visits the nodes in this tree in preorder.
*/
public void traversePreorder(Visitor visitor);
/**
* Visits the nodes in this tree in postorder.
*/
public void traversePostorder(Visitor visitor);
/**
* Visits the nodes in this tree in inorder.
*/
public void traverseInorder(Visitor visitor);
/**
* Simple visitor interface.
*/
public interface Visitor {
public void visit(BinaryTreeNode node);
}
}