-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBinaryTrees.java
151 lines (132 loc) · 3.08 KB
/
BinaryTrees.java
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
140
141
142
143
144
145
146
147
148
149
150
151
package algorithms;
import java.util.Iterator;
import java.util.LinkedList;
/**
* Binary Tree manipulations. Adapted from http://cslibrary.stanford.edu/110/BinaryTrees.html#java
*
* @author joeytawadrous
*/
public class BinaryTrees
{
private static Node root;
public static void main(String[] args)
{
Tree(2);
printTree(root);
System.out.println(isBST(root, 0, 9));
}
/**
* Creates a tree data structure.
* @param data
*/
public static void Tree(int data)
{
root = newNode(data);
root.leftChild = newNode(4);
root.rightChild = newNode(5);
insertNode(root.rightChild, 7);
System.out.println("Size: " + size(root, 0));
}
/**
* Inserts a new node into the tree.
* @param parent
* @param data
* @return node
*/
public static void insertNode(Node parent, int data)
{
// TODO: Improve insertion, i.e. check if entering node causes to break bst tree (entering node in wrong place).
Node node = newNode(data);
if(data <= parent.data)
{
parent.leftChild = node;
}
else
{
parent.rightChild = node;
}
}
/**
* Creates a new node.
* @return node
*/
public static Node newNode(int data)
{
Node node = new Node();
node.data = data;
node.leftChild = null;
node.rightChild = null;
return node;
}
/**
* Returns the size of a tree.
* @param node
* @param size
* @return size
*/
public static int size(Node node, int size)
{
if (node == null) { return(0); }
else
{
return(1 + size(node.leftChild, size) + size(node.rightChild, size));
}
}
/**
* Returns true if tree is binary search tree.
* @param node
* @param min
* @param max
* @return boolean
*/
public static boolean isBST(Node node, int min, int max)
{
if(node == null)
return true;
if(node.leftChild == null && node.rightChild == null)
return true;
else if(node.data > min && node.data < max)
return(isBST(node.leftChild, min, node.data) && isBST(node.rightChild,node.data,max));
else
return false;
}
/**
* Prints the tree.
* @param node
*/
public static void printTree(Node node)
{
LinkedList<Node> currentLevel = new LinkedList<Node>();
LinkedList<Node> nextLevel = new LinkedList<Node>();
currentLevel.add(node);
while (!currentLevel.isEmpty())
{
Iterator<Node> iter = currentLevel.iterator();
while (iter.hasNext())
{
Node currentNode = iter.next();
System.out.print(currentNode.data + " ");
if (currentNode.leftChild != null)
{
nextLevel.add(currentNode.leftChild);
}
if (currentNode.rightChild != null)
{
nextLevel.add(currentNode.rightChild);
}
}
System.out.println();
currentLevel = nextLevel;
nextLevel = new LinkedList<Node>();
}
}
/**
* Inner node class.
*/
private static class Node
{
private int data;
private Node leftChild;
private Node rightChild;
}
}