Breadth-first tree - java

I seem to be having issues structuring a breadth-first tree.
In the code below, I have a node that is inserted through a loop in another class.
The structure of the tree is supposed to be as so:
A
/ \
B C
/\ /\
D E F G
Now for the code:
My code structures the left side correctly, whereas the right side adds the left side as well. I understand where in the code this happens, but is there a way to prevent this from happening?
public Node familyTree;
public void breadthFirst(Node newNode){
familyTree = breadthFirst(familyTree,newNode);
}
public Node breadthFirst(Node T, Node newNode){
if(T == null){
T = newNode;
return T;
}
if(T.left == null){
newNode.height = T.height + 1;
T.left = newNode;
return T;
}
else if(T.right == null){
newNode.height = T.height + 1;
T.right = newNode;
return T;
}
else{
T.left = breadthFirst(T.left, newNode);
T.right = breadthFirst(T.right, newNode); <-- this is the corporate
}
return T;
}

if you are using recursive, definitely the implementation is a "depth-first-search", for breadth-first-search, you use a queue or a FIFO data structure
pseudo-code
public Node breadthFirst(Node T, Node searchNode){
Queue queue = new Queue();
queue.queue(T);
while (!queue.isEmpty()) {
Node curNode = queue.dequeue();
if (curNode == null) continue;
if (curNode.value().equals(searchNode.value()) {
return curNode;
}
queue.queue(curNode.left);
queue.queue(curNode.right);
}
return null; //or throw exception not found
}

I think a breadth-first tree resemble a complete binary tree, so you can employ Array to store it rather than link list. And about complete binary tree if the parent number is n then the left number=2*n+1 right=2*n+2.
for example: use array nodes[the amount of node]
and the 0th Node is A(number begin zero)
when the number n of Node is even like C(n=2) then nodes[(n-2)/2].right = nth node
else odd like B then nodes[(n-1)/2].left = nth node

What you are missing is using the height of the left and right node to determine which side the new node should be a child of when you reach the else statement. Currently, you're adding it to both sides regardless of where the node should be placed.
As an aside, it looks like you might be keeping track of depth of the tree in height attribute, rather than the height. This stackoverflow post does a good job of explaining the difference.

Related

Deleteing a Node in a Binary tree

Iunderstand the basis of a deletion algorithm in a Binary Search tree and have created the following code to delete the largest value from the tree.
public void DelLargest()
{
Node<T> del = this;
boolean child = this.left.empty();
boolean child2 = this.right.empty();
right.DelLargest();
if(child && child2)
this.head = null;
else if(child == true && child2 == false)
this.head = left;
}
Basically what I have is that the recursion runs until 'this' is the rightmost node and then checks two cases, whether 'this' is a leaf, or whether 'this' has a left child. (The other case normally associated with this kind of algorithm is redundant because in finding the node with the largest value, I have gone as right as I can go.) The trouble I am having is getting the current node to then either point to null or to the value at Node left.
Note : This is what my instructor referred to as a "modern" Binary search Tree wherein a vertex or "filled" node and a nil or "empty" node are two subclasses of Interface Node which define the characteristics of each type.
I've managed to narrow the problem down to the fact that I do not have a method that returns a value of a given Node. Working on that now, input would be appreciated.
As suggested in the other answer you should use iterative approach.
In a BST the largest value is the rightmost node.
So do a scan and keep going right until you hit a null.
In the scan keep track of three nodes. (gpnode, pnode, node).
Once the scan is done you will have (gpnode,pnode,null)
Now there are 2 cases.
case 1:
pnode is a leaf. So change the edge (gpnode,pnode) to (gpnode,null)
case 2: (EDITED)
pnode.lChild is not null. Note that pnode.rChild will be null as the search would have terminated at that point.
Now change the edge (gpnode,pnode) to (gpnode,pnode.lChild)
Here is the pseudo code:
public class Node
{
long key;
Node lChild;
Node rChild;
}
public void DelLargest()
{
Node gpnode = null;
Node pnode = null;
Node node = root;
while(node != null) // keep going right until a null is reached
{
gpnode = pnode;
pnode = node;
node = node.rChild;
}
if(pnode.lChild == null) // its a leaf node So change the edge (gpnode,pnode) to (gpnode,null)
{
if(gpnode.lChild == pnode)
{
gpnode.lChild = null;
}
else
{
gpnode.rChild = null;
}
}
else // copy lChild's key to this node and delete lChild
{
if(gpnode.lChild == pnode)
{
gpnode.lChild = pnode.lChild;
}
else
{
gpnode.rChild = pnode.lChild;
}
}
}
You've got the right idea. What you want to do is keep a reference to the right most nodes parent, and the right most nodes left child so then when you delete it you can attach the two.
Here's an iterative solution. This will generally be more efficient than recursion, but if you want recursion you should be able to adapt it:
public void delLargest() {
// get rightmost node's parent
Node<T> current = root;
while(current.right != null && current.right.right != null) {
current = current.right;
}
// get the rightmost nodes left node
Node<T> left = current.right.left;
// attach the parent and left
current.right = left;
// nothing points to the right most node anymore, so it will be garbage collected
}

With Binary Trees, I want to add the next node in consecutive order, my algorithm isn't quite working however

For instance, if I had
A
/ \
B C
/
D
I would want the next addition to be:
A
/ \
B C
/ \
D E
But I'm having a lot of trouble detecting where the next spot for the item to input will be. I have the following code:
public static BinaryTree<String> addToTree(BinaryTree<String> tree, String name) {
if (tree.getLeft() == null) {
BinaryTree<String> newTree = new BinaryTree<String>();
newTree.makeRoot(name);
tree.attachLeft(newTree);
}
else if (tree.getRight() == null) {
BinaryTree<String> newTree = new BinaryTree<String>();
newTree.makeRoot(name);
tree.attachRight(newTree);
}
// Both are non-null
else {
if (tree.getLeft().getLeft() == null || tree.getLeft().getRight() == null) {
tree.attachLeft(addToTree(tree.getLeft(), name));
}
else if (tree.getRight().getLeft() == null || tree.getRight().getRight() == null) {
tree.attachRight(addToTree(tree.getRight(), name));
}
}
return tree;
}
But it will only work for up to a three level tree. If I try to add the fourth, it no longer adds any.
How do I implement it so it will figure out where the next item is null, and then add it there?
I also thought of having a checkNullity() method, wherein I'd take a tree, and check if its children were null, but I was also having trouble figuring out how to get the children's children. I wanted to find where it was null and then add it there.
Could anyone offer some input?
You can modify breadth first traversal to accomplish this I think. When you pop up the items from the queue, check if any of the children is empty. The first empty child slot is the place you want to add to.
addNode(root, newNode)
q = empty queue
q.enqueue(root)
while not q.empty do
node := q.dequeue()
if node.left == null
//create new node as nodes left child
return
q.enqueue(node.left)
if node.right == null
//create new node as nodes right child
return
q.enqueue(node.right)
Since, you want to insert the element in the order from left to right and starting from the same level. I would suggest you to look in to Breath First Search. I have provided an basic implementation.
public void insert(child, root){
if (root == null){
root = child
}
Node iter = root
Myqueue q = new Myqueue(); //Implementation of the Java Queue Interface
while (iter!=null){
//Check: If the left node exists, enque in the que
if(iter.is_left()){
q.insert(iter.left)
}
else{
iter.left = child
iter = null
}
//Similary for the right
if(iter.is_right()){
q.insert(iter.right)
}
else{
iter.right = child
iter = null
}
if (iter != null){
iter = q.poll() //Retreiving the head of the queue
}
}
}
You could enumerate all nodes while adding them to the tree. If you want to add the n-th node to the tree, it'll be a child of the n/2-th node: left if n%2 == 0 and right if n%2 == 1.
This certainly creates the tree you are asking for although I am still not sure it is what you want:
public class BinaryTree<T> {
T root = null;
BinaryTree<T> left = null;
BinaryTree<T> right = null;
public BinaryTree<T> getLeft() {
return left;
}
public BinaryTree<T> getRight() {
return right;
}
public void makeRoot(T root) {
this.root = root;
}
public void attachLeft(BinaryTree<T> tree) {
left = tree;
}
public void attachRight(BinaryTree<T> tree) {
right = tree;
}
public static BinaryTree<String> addToTree(BinaryTree<String> tree, String name) {
if (tree.getLeft() == null) {
BinaryTree<String> newTree = new BinaryTree<String>();
newTree.makeRoot(name);
tree.attachLeft(newTree);
} else if (tree.getRight() == null) {
BinaryTree<String> newTree = new BinaryTree<String>();
newTree.makeRoot(name);
tree.attachRight(newTree);
} else {
addToTree(tree.getLeft(), name);
}
return tree;
}
public static void main(String[] args) {
try {
BinaryTree<String> tree = new BinaryTree<String>();
String add = "ABCDEFG";
tree.makeRoot(add.substring(0, 1));
for (int i = 1; i < add.length(); i++) {
addToTree(tree, add.substring(i, i + 1));
}
System.out.println("Done");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
Added
I have clearly misunderstood the question. Perhaps an example will help.
If I added one character at a time (as strings) from the following string what would you expect?
"ABCDEFG"
A
/ \
B C
/ \ | \
D E F G?
or something else.
What would you then expect from
"ADEFGBC"
A
/ \
D E
/ \ | \
F G B C
or
A
/ \
B C
/ \ | \
D E F G
or something else?
Either is possible but I cannot see any value in either case.
In order to add an element to a proper place in a binary tree, you have to go from the root at at each node answer the following question: Should I descend to the left or to the right subtree? This is what your problem boils down to - how to make this decision at each node.
You start is OK. If the node has no left subtree, then the newly added leaf should be its left child. And if the node has a left subtree but no right subtree, then the newly added leaf should be its right child.
But how to decide if the node has both subtrees? For this you'll need to keep some sort of information at the nodes that you can use to decide. One possibility is to keep at each node the total size of its subtree. Then if both subtrees have the same size, it means both are perfectly balanced and so you add to the left. Otherwise, if the left subtree has size 2^n-1 it means that it's balanced (and the right one is not) so you add to the right. If not, add to the left.
However, you can do much simpler than that. Since your trees always keep this structure, you can represent a tree as an ArrayList. The root node is at index 0 and for a node at index n its children are at indexes _2*n+1_ and _2*n+2_. This is just how binary heaps are implemented. This way, you'll get O(1) time complexity for adding a new node - simply append it at the end of the list. (However, if you need some classical tree operations like rotations, this implementation won't work.)

Level-order tree traversal

I am trying to write a method that will take an IntTree as a parameter and return a Queue with the values of the IntTree in level order. To clarify: an IntTree is a tree with an integer value as its root, and has an IntTree as its left and right subtrees.
A note on some methods:
value(t) - returns the integer value at the root of the tree
left(t) - returns the left IntTree subtree //
right(t) - returns the right sub-tree
Here's the code I have so far. I'm rather new to programming, so I'm sorry if it's not very elegant.
public static QueueList levelOrder(IntTree t) {
//returns a QueueList with the values in level order
Object val;
QueueList q = new QueueList(); //temp queueList
QueueList theta = new QueueList(); //the QueueList which will eventually be printed
if (isEmpty(t)) {
return theta; //possible problem here
} else {
IntTree tL = left(t); //using for possible updating. probably won't work
IntTree tR = right(t);
q.enqueue(value(t)); //enqueue the root of the tree
while (!q.isEmpty()) {
val = q.dequeue(); //pop off the top element for printing
theta.enqueue(val); // put the element in the queue to be printed
if (tL != null) {
q.enqueue(value(tL)); //if the left isn't null, enqueue the lefts
tL = left(tL); //this will only branch left
}
if (tR != null) { //if the right isn't null, enqueue the rights
q.enqueue(value(tR));
tR = right(tR); //this will only branch right
}
}
}
return theta; //returns a queue with values in order
}
I wrote the tL and tR variables because if I wrote something like "if (left(t) != null)", I would end up with infinite recursion, since 't' was never updated. The problem with this code is that 'tL' will only branch left and 'tR' will only branch right. So after one level below the root, some values will never be stored. I hope this was clear, and any help is greatly appreciated. Thank you.
Instead of implementing the fringe as a queue of values, implement it as a queue of IntTrees (nodes). That will simplify your logic tremendously.
while (!q.isEmpty()) {
IntTree node = q.dequeue(); //pop off the top element
theta.enqueue(value(node)); // put the element in the queue to be printed
//enqueue the children
IntTree left = left(node);
if ( left != null ) {
q.enqueue(left);
}
//...similar for right
}
When you have this you don't have to maintain the tL and tR pointers in parallel, which I imagine is a flawed approach anyhow.
Links
Breadth-First Traversal (Wikipedia)

How do I iterate over Binary Tree?

Right now I have
private static void iterateall(BinaryTree foo) {
if(foo!= null){
System.out.println(foo.node);
iterateall(foo.left);
iterateall(foo.right);
}
}
Can you change it to Iteration instead of a recursion?
What you're looking for is a successor algorithm.
Here's how it can be defined:
First rule: The first node in the tree is the leftmost node in the tree.
Next rule: The successor of a node is:
Next-R rule: If it has a right subtree, the leftmost node in the right subtree.
Next-U rule: Otherwise, traverse up the tree
If you make a right turn (i.e. this node was a left child), then that parent node is the successor
If you make a left turn (i.e. this node was a right child), continue going up.
If you can't go up anymore, then there's no successor
As you can see, for this to work, you need a parent node pointer.
Example:
First rule: The first node in the tree is the leftmost node in the tree: (1)
Next-U rule: Since (1) has no right subtree, we go up to (3). This is a right turn, so (3) is next.
Next-R rule: Since (3) has a right subtree, the leftmost node in that subtree is next: (4).
Next-U rule: Since (4) has no right subtree, we go up to (6). This is a right turn, so next is (6).
Next-R rule: Since (6) has a right subtree, the leftmost node in that subtree is next: (7).
Next-U rule: Since (7) has no right subtree, we go up to (6). This is a left turn, so we continue going up to (3). This is a left turn, so we continue going up to (8). This is a right turn, so next is (8).
Next-R rule: Since (8) has a right subtree, the leftmost node in that subtree is next: (10).
Next-R rule: Since (10) has a right subtree, the leftmost node in that subtree is next: (13).
Next-U rule: Since (13) has no right subtree, we go up to (14). This is a right turn, so next is (14).
Next-U rule: Since (14) has no right subtree, we go up to (10). This is a left turn, so we continue going up to (8). This is a left turn, so we want to continue going up, but since (8) has no parent, we've reached the end. (14) has no successor.
Pseudocode
Node getLeftMost(Node n)
WHILE (n.leftChild != NULL)
n = n.leftChild
RETURN n
Node getFirst(Tree t)
IF (t.root == NULL) RETURN NULL
ELSE
RETURN getLeftMost(t.root);
Node getNext(Node n)
IF (n.rightChild != NULL)
RETURN getLeftMost(n.rightChild)
ELSE
WHILE (n.parent != NULL AND n == n.parent.rightChild)
n = n.parent;
RETURN n.parent;
PROCEDURE iterateOver(Tree t)
Node n = getFirst(t);
WHILE n != NULL
visit(n)
n = getNext(n)
Java code
Here's a simple implementation of the above algorithm:
public class SuccessorIteration {
static class Node {
final Node left;
final Node right;
final int key;
Node parent;
Node(int key, Node left, Node right) {
this.key = key;
this.left = left;
this.right = right;
if (left != null) left.parent = this;
if (right != null) right.parent = this;
}
Node getLeftMost() {
Node n = this;
while (n.left != null) {
n = n.left;
}
return n;
}
Node getNext() {
if (right != null) {
return right.getLeftMost();
} else {
Node n = this;
while (n.parent != null && n == n.parent.right) {
n = n.parent;
}
return n.parent;
}
}
}
}
Then you can have a test harness like this:
static Node C(int key, Node left, Node right) {
return new Node(key, left, right);
}
static Node X(int key) { return C(key, null, null); }
static Node L(int key, Node left) { return C(key, left, null); }
static Node R(int key, Node right) { return C(key, null, right); }
public static void main(String[] args) {
Node n =
C(8,
C(3,
X(1),
C(6,
X(4),
X(7)
)
),
R(10,
L(14,
X(13)
)
)
);
Node current = n.getLeftMost();
while (current != null) {
System.out.print(current.key + " ");
current = current.getNext();
}
}
This prints:
1 3 4 6 7 8 10 13 14
See also
Complete Java listing and output on ideone.com
Can you change it to Iteration instead of a recursion?
You can, using an explicit stack. Pseudocode:
private static void iterateall(BinaryTree foo) {
Stack<BinaryTree> nodes = new Stack<BinaryTree>();
nodes.push(foo);
while (!nodes.isEmpty()) {
BinaryTree node = nodes.pop();
if (node == null)
continue;
System.out.println(node.node);
nodes.push(node.right);
nodes.push(node.left);
}
}
But this isn’t really superior to the recursive code (except for the missing base condition in your code).
Sure, you have two general algorithms, depth first search and breadth first search.
If order of traversal is not important to you, go for breadth first, it's easier to implement for iteration. You're algorithm should look something like this.
LinkedList queue = new LinkedList();
queue.add(root);
while (!queue.isEmpty()){
Object element = queue.remove();
queue.add(element.left);
queue.add(element.right);
// Do your processing with element;
}
As with every recursion, you can use additional data structure - i.e. the stack.
A sketch of the solution:
private static void visitall(BinaryTree foo) {
Stack<BinaryTree> iterationStack = new Stack<BinaryTree>();
iterationStack.push(foo);
while (!iterationStack.isEmpty()) {
BinaryTree current = iterationStack.pop();
System.out.println(current.node);
current.push(current.right); // NOTE! The right one comes first
current.push(current.left);
}
}
I had a tree (not binary) and eventually solved it with this very simple algorithm. The other solutions used left and right that were not relevant or even implemented in the examples.
My structure was: nodes with each parent containing list of children, and each child containing a pointer back to the parent. Pretty common...
After a bunch of refactoring, I came up with the following example using Kotlin. It should be trivial to convert to your language of choice.
Helper Functions
First, the node must provide 2 simple functions. This will vary depending on your Node class' implementation:
leftMost - This is the first child node. If that node has children, it's first child, etc. If no children, return this.
fun leftMost(): Node {
if (children.isEmpty()) {
return this
}
var n = this
while (n.children.isNotEmpty()) {
n = n.children[0]
}
return n
}
nextSibling - The next sibling of this node, or NULL
fun nextSibling(): Node? {
if (parent == null) return null
val siblingIndex = parent.children.indexOf(this) + 1
return if (siblingIndex < parent.children.size) {
parent.children[siblingIndex]
} else {
null
}
}
The Iteration
The iteration starts with the leftMost of the root.
Then inspect the next sibling.
If NOT NULL the sibling's leftMostChild
If NULL, the parent, and if the parent is NULL, we are done.
That's it.
Here is a Kotlin iterator function.
fun iterator(): Iterator<Node> {
var next: Node? = this.leftMost()
return object : Iterator<Node> {
override fun hasNext(): Boolean {
return next != null
}
override fun next(): Node {
val ret = next ?: throw NoSuchElementException()
next = ret.nextSibling()?.leftMost() ?: ret.parent
return ret
}
}
}
Here is the same next() function, but without the Kotlin shorthand for dealing with NULL values, for those that are not hip to the syntax.
fun next(): Node {
val ret = next
if (ret == null) throw NoSuchElementException()
val nextSibling = ret.nextSibling()
if (nextSibling != null) {
next = nextSibling.leftMost()
}
else {
next = ret.parent
}
return ret
}
Yes, you can change it to iteration instead of a recursion, but then it gets much more complicated, since you need to have some way to remember where to go back from the current node. In the recursive case, the Java call stack handles that, but in an iterative solution you need to build your own stack, or perhaps store back pointers in the nodes.

Java, Binary tree remove method

I am trying to write a remove(node cRoot, Object o) function for a sorted binary tree.
Here is what I have so far:
private boolean remove(Node cRoot, Object o) {
if (cRoot == null) {
return false;
}
else if (cRoot.item.equals(o)) {
//erase node fix tree
return true;
}
else if (((Comparable)item).compareTo(cRoot.item)<=0){
return remove(cRoot.lChild, o);
}
else {
return remove(cRoot.rChild,o);
}
}
It does not work correctly. To delete a node you have to repair the tree to fix the hole. How should this be done?
There are generally two ways of performing a remove on the tree:
First method:
Remove the node, then replace it with either child. Then, resort the tree by doing parent-child swapping until the tree is once again sorted.
The second method:
Traverse the tree to find the next (highest or lowest) value that belongs as the root*, if it is a leaf node, swap that with the root, then trim off the value you want to remove. If it is an internal node, you will have to recursively call remove on that node. Repeat until a leaf node is removed.
*What I mean is, if you convert your BST into a sorted list, then you will want to pick either value to the left or right of the root as the new root. I.e. leftmost child of the right subtree, or right most child of the left subtree.
The basic pseudo-code for erasing a node from a sorted tree is pretty simple:
erase the node value
find child node with maximum value
make it the root node
if it had children - goto 2 recursively
Basically what you are doing is bubbling nodes up the tree, each time the maximum of the children node in each node, so that in the end you stay with a sorted tree, and only one node missing at the end of the full path you went.
Also - see wikipedia on the subject, they have some sample code in C as well.
In the simple case3 you can use next algorithm:
if(removed node had left child)
{
place left child instead of removed node;
most_right = most right leaf in the left subtree;
move right child of removed node as right child of most_right;
}
else
{
place right child instead of removed node
}
In more complicated case you may need to rebalance your tree (see AVL trees, http://www.cmcrossroads.com/bradapp/ftp/src/libs/C++/AvlTrees.html for C++ example)
leaf-delete node
1-child Promote the subtree
2-child case replace the node with either
in order successor or predecessor
left most of the right subtree or
right most of the left subtree
I found this code on Habrahabr. I've just added comments.
public void remove (T1 k){
Node<T1,T2> x = root, y = null;
// from while to if(x == null) - searching for key
while(x != null){
int cmp = k.compareTo(x.key);
if(cmp == 0){
break; // quit cycle if key element is found
} else {
y = x;
if(cmp < 0){
x = x.left;
} else {
x = x.right;
}
}
}
if(x == null) return; // if key is not found or tree is empty
if(x.right == null){ // if element found has not right child
if(y == null){ // if element found is root & has not right child
root = x.left;
} else { // if element found is not root & has not right child
if(x == y.left) y.left = x.left;
else y.right = x.left;
}
} else { // element found has right child, so search for most left of rights
Node<T1,T2> mostLeft = x.right;
y = null;
while(mostLeft.left != null) {
y = mostLeft;
mostLeft = mostLeft.left;
}
if(y == null){ // if right child of element found has not left child
x.right = mostLeft.right;
} else { // if right child of element found has left child
y.left = mostLeft.right;
}
x.key = mostLeft.key;
x.value = mostLeft.value;
}
}

Categories