Repository navigation
Expand file tree
/
Copy pathNode.java
More file actions
89 lines (83 loc) · 2.29 KB
/
Copy pathNode.java
File metadata and controls
89 lines (83 loc) · 2.29 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
/**
* Creates a Node with content and two children, a left and a right one, each of which contains an adress to a new Node.
* If said adress is null, that means the Node is a leaf and the Tree it's in can't be traversed any further.
* @version 1.0
* @author S. Larsson<br> A. Ebeling<br> M. Tijanic
*/
public class Node
{
/**
* Element in Node which is an Item. This is the content of the Node and where information can be stored.
*
*/
public Item element;
/**
* Used to traverse. Stores an adress to the next Node.
*/
public Node leftChild;
/**
* Used to traverse. Stores an adress to the next Node.
*/
public Node rightChild;
/**
* Class constructor which takes no arguments.
*/
public Node()
{
this( null );
}
/**
* Class constructor, takes Item as argument.
* @param theElement Item with information.
*/
public Node( Item theElement )
{
this( theElement, null, null );
}
/**
* Class constructor, takes an Item and two Nodes as argument to create a branch or leaf in MakeTree.
*
* @param theElement Item with information.
* @param lt Create a left child which is a Node. A null value means won't traverse further.
* @param rt Create a ight child which is a Node. A null value means won't traverse further.
*/
public Node( Item theElement, Node lt, Node rt )
{
element = theElement;
leftChild = lt;
rightChild = rt;
}
/**
* Prints pre-order with no return.
*/
public void printPreOrder( )
{
System.out.println( element ); // Node
if( leftChild != null )
leftChild.printPreOrder( ); // Left
if( rightChild != null )
rightChild.printPreOrder( ); // Right
}
/**
* Prints post-order with no return.
*/
public void printPostOrder( )
{
if( leftChild != null )
leftChild.printPostOrder( ); // Left
if( rightChild != null )
rightChild.printPostOrder( ); // Right
System.out.println( element ); // Node
}
/**
* Print in order with no return.
*/
public void printInOrder( )
{
if( leftChild != null )
leftChild.printInOrder( ); // Left
System.out.println( element ); // Node
if( rightChild != null )
rightChild.printInOrder( ); // Right
}
}