Sentinel node in Binary Search Trees - algorithm

I was wondering if in a way to avoid having to deal with the root as a special case in a Binary Search Tree I could use some sort of sentinel root node?
public void insert(int value) {
if (root == null) {
root = new Node(value);
++size;
} else {
Node node = root;
while (true) {
if (value < node.value) {
if (node.left == null) {
node.left = new Node(value);
++size;
return;
} else {
node = node.left;
}
} else if (value > node.value) {
if (node.right == null) {
node.right = new Node(value);
++size;
return;
} else {
node = node.right;
}
} else return;
}
}
}
For instance, in the insert() operation I have to treat the root node in a special way. In the delete() operation the same will happen, in fact, it will be way worse.
I've thought a bit regarding the issue but I couldn't come with any good solution. Is it because it is simply not possible or am I missing something?

The null node itself is the sentinel, but instead of using null, you can use an instance of a Node with a special flag (or a special subclass), which is effectively the null node. A Nil node makes sense, as that is actually a valid tree: empty!
And by using recursion you can get rid of the extra checks and new Node littered all over (which is what I presume is really bothering you).
Something like this:
class Node {
private Value v;
private boolean is_nil;
private Node left;
private Node right;
public void insert(Value v) {
if (this.is_nil) {
this.left = new Node(); // Nil node
this.right = new Node(); // Nil node
this.v = v;
this.is_nil = false;
return;
}
if (v > this.v) {
this.right.insert(v);
} else {
this.left.insert(v);
}
}
}
class Tree {
private Node root;
public Tree() {
root = new Node(); // Nil Node.
}
public void insert(Value v) {
root.insert(v);
}
}
If you don't want to use recursion, your while(true) is kind of a code smell.
Say we keep it as null, we can perhaps refactor it as.
public void insert(Value v) {
prev = null;
current = this.root;
boolean left_child = false;
while (current != null) {
prev = current;
if (v > current.v) {
current = current.right;
left_child = false;
} else {
current = current.left;
left_child = true;
}
}
current = new Node(v);
if (prev == null) {
this.root = current;
return;
}
if (left_child) {
prev.left = current;
} else {
prev.right = current;
}
}

The root will always be a special case. The root is the entry point to the binary search tree.
Inserting a sentinel root node means that you will have a root node that is built at the same time as the tree. Furthermore, the sentinel as you mean it will just decrease the balance of the tree (the BST will always be at the right/left of its root node).
The only way that pops in my mind to not manage the root node as a special case during insert/delete is to add empty leaf nodes. In this way you never have an empty tree, but instead a tree with an empty node.
During insert() you just replace the empty leaf node with a non-empty node and two new empty leafs (left and right).
During delete(), as a last step (if such operation is implemented as in here) you just empty the node (it becomes an empty leaf) and trim its existing leafs.
Keep in mind that if you implement it this way you will have more space occupied by empty leaf nodes than by nodes with meaningful data. So, this implementation has sense only if space is not an issue.
The code would look something like this:
public class BST {
private Node root;
public BST(){
root = new Node();
}
public void insert(int elem){
root.insert(elem);
}
public void delete(int elem){
root.delete(elem);
}
}
public class Node{
private static final int EMPTY_VALUE = /* your empty value */;
private int element;
private Node parent;
private Node left;
private Node right;
public Node(){
this(EMPTY_VALUE, null, null, null);
}
public Node(int elem, Node p, Node l, Node r){
element = elem;
parent = p;
left = l;
right = r;
}
public void insert(int elem){
Node thisNode = this;
// this cycle goes on until an empty node is found
while(thisNode.element != EMPTY_VALUE){
// follow the correct path for the insertion here
}
// insert new element here
// thisNode is an empty node at this point
thisNode.element = elem;
thisNode.left = new Node();
thisNode.right = new Node();
thisNode.left.parent = thisNode;
thisNode.right.parent = thisNode;
}
public void delete(int elem){
// manage delete here
}
}

Related

Randomised Path on graph - set length, no crossing, no dead ends

I am working on a game with a 8 wide 5 high grid. I have a 'snake' feature which needs to enter the grid and "walk" around for a set distance (20 for example). There are certain restrictions for the movement of the snake:
It needs go over the predetermined amount of blocks (20)
It cannot go over itself or double back (no dead ends)
Currently I am using a Randomised Depth First search, however I have found that it occasionally goes back over itself (crosses its own path) and am not sure if this is the best way to go about it.
Options considered: I have looked at using A*, but am struggling to figure out a good way to do it without a predetermined goal and the conditions above. I have also considered adding a heuristic to favour blocks that are not on the outside of the grid - but am not sure either of these will solve the issue at hand.
Any help is appreciated and I can add more detail or code if necessary:
public List<GridNode> RandomizedDepthFirst(int distance, GridNode startNode)
{
Stack<GridNode> frontier = new Stack<GridNode>();
frontier.Push(startNode);
List<GridNode> visited = new List<GridNode>();
visited.Add(startNode);
while (frontier.Count > 0 && visited.Count < distance)
{
GridNode current = frontier.Pop();
if (current.nodeState != GridNode.NodeState.VISITED)
{
current.nodeState = GridNode.NodeState.VISITED;
GridNode[] vals = current.FindNeighbours().ToArray();
List<GridNode> neighbours = new List<GridNode>();
foreach (GridNode g in vals.OrderBy(x => XMLReader.NextInt(0,0)))
{
neighbours.Add(g);
}
foreach (GridNode g in neighbours)
{
frontier.Push(g);
}
if (!visited.Contains(current))
{
visited.Add(current);
}
}
}
return visited;
}
An easy way to account for back tracking is using a recursive dfs search.
Consider the following graph:
And a java implementation of a dfs search, removing nodes from the path when backtracking (note the comments. Run it online here) :
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;
public class Graph {
//all graph nodes
private Node[] nodes;
public Graph(int numberOfNodes) {
nodes = new Node[numberOfNodes];
//construct nodes
for (int i = 0; i < numberOfNodes; i++) {
nodes[i] = new Node(i);
}
}
// add edge from a to b
public Graph addEdge(int from, int to) {
nodes[from].addNeighbor(nodes[to]);
//unless unidirectional: //if a is connected to b
//than b should be connected to a
nodes[to].addNeighbor(nodes[from]);
return this; //makes it convenient to add multiple edges
}
//returns a list of path size of pathLength.
//if path not found : returns an empty list
public List<Node> dfs(int pathLength, int startNode) {
List<Node> path = new ArrayList<>(); //a list to hold all nodes in path
Stack<Node> frontier = new Stack<>();
frontier.push(nodes[startNode]);
dfs(pathLength, frontier, path);
return path;
}
private boolean dfs(int pathLength, Stack<Node> frontier, List<Node> path) {
if(frontier.size() < 1) {
return false; //stack is empty, no path found
}
Node current = frontier.pop();
current.setVisited(true);
path.add(current);
if(path.size() == pathLength) {
return true; //path size of pathLength found
}
System.out.println("testing node "+ current); //for testing
Collections.shuffle(current.getNeighbors()); //shuffle list of neighbours
for(Node node : current.getNeighbors()) {
if(! node.isVisited()) {
frontier.push(node);
if(dfs(pathLength, frontier, path)) { //if solution found
return true; //return true. continue otherwise
}
}
}
//if all neighbours tested and no solution found, current node
//is not part of the path
path.remove(current); // remove it
current.setVisited(false); //this accounts for loops: you may get to this node
//from another edge
return false;
}
public static void main(String[] args){
Graph graph = new Graph(9); //make graph
graph.addEdge(0, 4) //add edges
.addEdge(0, 1)
.addEdge(1, 2)
.addEdge(1, 4)
.addEdge(4, 3)
.addEdge(2, 3)
.addEdge(2, 5)
.addEdge(3, 5)
.addEdge(1, 6)
.addEdge(6, 7)
.addEdge(7, 8);
//print path with length of 6, starting with node 1
System.out.println( graph.dfs(6,1));
}
}
class Node {
private int id;
private boolean isVisited;
private List<Node>neighbors;
Node(int id){
this.id = id;
isVisited = false;
neighbors = new ArrayList<>();
}
List<Node> getNeighbors(){
return neighbors;
}
void addNeighbor(Node node) {
neighbors.add(node);
}
boolean isVisited() {
return isVisited;
}
void setVisited(boolean isVisited) {
this.isVisited = isVisited;
}
#Override
public String toString() {return String.valueOf(id);} //convenience
}
Output:
testing node 1
testing node 6
testing node 7
testing node 8
testing node 2
testing node 5
testing node 3
testing node 4
[1, 2, 5, 3, 4, 0]
Note that nodes 6,7,8 which are dead-end, are tested, but not included in the final path.

Modifying depth-first search

(source, destination) and it's type (tree, back, forward, cross)?
Here you go. Code in Java
import java.util.ArrayList;
import java.util.List;
class Node {
public String name;
public List<Node> connections = new ArrayList<>();
boolean visited = false;
Node(String name) {
this.name = name;
}
}
class DFS {
// Main part.
public static void search(Node root) {
if (root == null) {
return;
}
root.visited = true;
for (Node node : root.connections) {
if (!node.visited) {
// Print result.
System.out.println(root.name + "->" + node.name);
search(node);
}
}
}
}
public class App {
public static void main(String[] args) {
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
Node d = new Node("d");
Node e = new Node("e");
a.connections.add(b);
b.connections.add(a);
b.connections.add(c);
b.connections.add(d);
c.connections.add(b);
c.connections.add(d);
d.connections.add(b);
d.connections.add(c);
d.connections.add(e);
DFS.search(d);
}
}
Nice question.
This is the solution based on the source you posted as comment.
IMPORTANT: There is an error on the start/end table, third row third column should be "end[u] < end[v]" instead of "end[u] > end[v]"
void main(G, s){
for each node v in G{
explored[v]=false
parent[v]=null
start[v]=end[v]=null
}
Global clock = 0
DFS(G, s, s)
}
void DFS(G, s, parent){
explored[s] = true;
parent[s] = parent
start[s]=clock
clock++
for each u=(s,v){
if(explored[v] == false){
DFS(G, v)
}
print (s + "-->" + v +"type: " + getType(s,v))
}
end[s]=clock
clock++
}
String getType(s, v){
if(start[s]<start[v]){
if(end[s]>end[v]) return "Tree edge"
else return "Forward edge"
else{
if(end[s]<end[v]) return "Back edge"
else return "Cross edge"
}
}

Why do I need the visited state in Breadth First Search?

I've implemented a breadth first search algorithm (actually, it's breadth first traversal because I'm not searching for any particular node, simply printing out the node values in the order they are visited) and haven't used any state tracking of each node - i.e. I haven't marked any node as visited. In most BFS implementations I see this notion of marking a node as visited so that you never visit it twice, but in my implementation I can't see any case where this would be possible.
Could someone explain why the visited state is ever useful and/or necessary?
Here's my implementation:
import java.util.LinkedList;
import java.util.Queue;
public class BFS {
public static void printTree(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while(queue.isEmpty() == false) {
Node curr = queue.remove();
System.out.println(curr.getValue());
if (curr.getLeft() != null) {
queue.add(curr.getLeft());
}
if (curr.getRight() != null) {
queue.add(curr.getRight());
}
}
}
public static void main(String[] args) {
Node leaf1 = new Node(5);
Node leaf2 = new Node(6);
Node leaf3 = new Node(7);
Node leaf4 = new Node(7);
Node leaf5 = new Node(11);
Node rightRightRoot = new Node(12, leaf4, leaf5);
Node leftRoot = new Node(1, leaf1, leaf2);
Node rightRoot = new Node(9, leaf3, rightRightRoot);
Node root = new Node(4, leftRoot, rightRoot);
printTree(root);
}
static class Node {
private int value;
private Node left, right;
public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}
public Node(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
}
}
Most BFS implementations you`ve seen traverse arbitrary graphs and you travel through the tree. The difference in those two cases are cycles. Arbitrary graph can have them and states are necessary for not putting the node into the queue twice, but in your case you may live without them.
Your code makes reference to a tree. By definition, a tree can't have cycles, so you are right that you don’t need to track if vertices have been visited if you are traversing a tree. A graph can have cycles, so it enables multiple vertices to be legitimate parents of a vertex.
For example, with a graph of four vertices (A,B,C,D) and the following topology:
A
/ \
B C
\ /
D
If a breadth-first search is started from A, both B & C can reach D. During execution, both B & C will add D to the queue, and thus it will be visited and printed twice.
The following code will print 4 twice:
Node D = new Node(4);
Node C = new Node(3, D, null);
Node B = new Node(2, null, D);
Node A = new Node(1, B, C);
printTree(A);

BST to Double-LinkedList Algorithm complexity?

I am looking at the code to do this in CC150. One of its method is as follows, it does this by retrieving the tail of the left sub-tree.
public static BiNode convert(BiNode root) {
if (root == null) {
return null;
}
BiNode part1 = convert(root.node1);
BiNode part2 = convert(root.node2);
if (part1 != null) {
concat(getTail(part1), root);
}
if (part2 != null) {
concat(root, part2);
}
return part1 == null ? root : part1;
}
public static BiNode getTail(BiNode node) {
if (node == null) {
return null;
}
while (node.node2 != null) {
node = node.node2;
}
return node;
}
public static void concat(BiNode x, BiNode y) {
x.node2 = y;
y.node1 = x;
}
public class BiNode {
public BiNode node1;
public BiNode node2;
public int data;
public BiNode(int d) {
data = d;
}
}
What I don't understand is the Time Complexity the author gives in the book O(n^2). What I came up with is T(N) = 2*T(N/2) + O(N/2), O(N/2) is consumed by the getting tail reference because it needs to traverse a list length of O(N/2). So by Master Theorem, it should be O(NlogN). Did I do anything wrong? Thank you!
public static BiNode convert(BiNode root) {//worst case BST everything
if (root == null) { // on left branch (node1)
return null;
}
BiNode part1 = convert(root.node1);//Called n times
BiNode part2 = convert(root.node2);//Single call at beginning
if (part1 != null) {
concat(getTail(part1), root);// O(n) every recursive call
} // for worst case so 1 to n
// SEE BELOW
if (part2 != null) {
concat(root, part2);
}
return part1 == null ? root : part1;
}
public static BiNode getTail(BiNode node) {//O(n)
if (node == null) {
return null;
}
while (node.node2 != null) {
node = node.node2;
}
return node;
}
public static void concat(BiNode x, BiNode y) {//O(1)
x.node2 = y;
y.node1 = x;
}
SAMPLE TREE:
4
/
3
/
2
/
1
As you can see, in the worst case scenario (Big-Oh is not average case), the BST would be structured using only the node1 branch(es). Thus, the recursion would have a have to run getTail() with '1 + 2 + ... + N' problem sizes to complete the conversion.
Which is O(n^2)

Is it possible to design a tree where nodes have infinitely many children?

How can design a tree with lots (infinite number) of branches ?
Which data structure we should use to store child nodes ?
You can't actually store infinitely many children, since that won't fit into memory. However, you can store unboundedly many children - that is, you can make trees where each node can have any number of children with no fixed upper bound.
There are a few standard ways to do this. You could have each tree node store a list of all of its children (perhaps as a dynamic array or a linked list), which is often done with tries. For example, in C++, you might have something like this:
struct Node {
/* ... Data for the node goes here ... */
std::vector<Node*> children;
};
Alternatively, you could use the left-child/right-sibling representation, which represents a multiway tree as a binary tree. This is often used in priority queues like binomial heaps. For example:
struct Node {
/* ... data for the node ... */
Node* firstChild;
Node* nextSibling;
};
Hope this helps!
Yes! You can create a structure where children are materialized on demand (i.e. "lazy children"). In this case, the number of children can easily be functionally infinite.
Haskell is great for creating "functionally infinite" data structures, but since I don't know a whit of Haskell, here's a Python example instead:
class InfiniteTreeNode:
''' abstract base class for a tree node that has effectively infinite children '''
def __init__(self, data):
self.data = data
def getChild(self, n):
raise NotImplementedError
class PrimeSumNode(InfiniteTreeNode):
def getChild(self, n):
prime = getNthPrime(n) # hypothetical function to get the nth prime number
return PrimeSumNode(self.data + prime)
prime_root = PrimeSumNode(0)
print prime_root.getChild(3).getChild(4).data # would print 18: the 4th prime is 7 and the 5th prime is 11
Now, if you were to do a search of PrimeSumNode down to a depth of 2, you could find all the numbers that are sums of two primes (and if you can prove that this contains all even integers, you can win a big mathematical prize!).
Something like this
Node {
public String name;
Node n[];
}
Add nodes like so
public Node[] add_subnode(Node n[]) {
for (int i=0; i<n.length; i++) {
n[i] = new Node();
p("\n Enter name: ");
n[i].name = sc.next();
p("\n How many children for "+n[i].name+"?");
int children = sc.nextInt();
if (children > 0) {
Node x[] = new Node[children];
n[i].n = add_subnode(x);
}
}
return n;
}
Full working code:
class People {
private Scanner sc;
public People(Scanner sc) {
this.sc = sc;
}
public void main_thing() {
Node head = new Node();
head.name = "Head";
p("\n How many nodes do you want to add to Head: ");
int nodes = sc.nextInt();
head.n = new Node[nodes];
Node[] n = add_subnode(head.n);
print_nodes(head.n);
}
public Node[] add_subnode(Node n[]) {
for (int i=0; i<n.length; i++) {
n[i] = new Node();
p("\n Enter name: ");
n[i].name = sc.next();
p("\n How many children for "+n[i].name+"?");
int children = sc.nextInt();
if (children > 0) {
Node x[] = new Node[children];
n[i].n = add_subnode(x);
}
}
return n;
}
public void print_nodes(Node n[]) {
if (n!=null && n.length > 0) {
for (int i=0; i<n.length; i++) {
p("\n "+n[i].name);
print_nodes(n[i].n);
}
}
}
public static void p(String msg) {
System.out.print(msg);
}
}
class Node {
public String name;
Node n[];
}
I recommend you to use a Node class with a left child Node and right child Node and a parent Node.
public class Node
{
Node<T> parent;
Node<T> leftChild;
Node<T> rightChild;
T value;
Node(T val)
{
value = val;
leftChild = new Node<T>();
leftChild.parent = this;
rightChild = new Node<T>();
rightChild.parent = this;
}
You can set grand father and uncle and sibling like this.
Node<T> grandParent()
{
if(this.parent.parent != null)
{
return this.parent.parent;
}
else
return null;
}
Node<T> uncle()
{
if(this.grandParent() != null)
{
if(this.parent == this.grandParent().rightChild)
{
return this.grandParent().leftChild;
}
else
{
return this.grandParent().rightChild;
}
}
else
return null;
}
Node<T> sibling()
{
if(this.parent != null)
{
if(this == this.parent.rightChild)
{
return this.parent.leftChild;
}
else
{
return this.parent.rightChild;
}
}
else
return null;
}
And is impossible to have infinite child, at least you have infinite memory.
good luck !
Hope this will help you.

Resources