Copying a Binary Tree in Java using a preorder traversal - java

I am trying to copy a binary tree using the pre order traversal but I am stuck.
As I am not putting any of the values into a new tree they are obviously not copying correctly...
public class Node{
int key;
String name;
Node leftChild;
Node rightChild;
Node(int key, String name){
this.key = key;
this.name = name;
}
public class BinaryTree{
public Node root;
public void copyTree(Node focusNode){
if(focusNode != null){
Node copyNode = new Node(focusNode.key, focusNode.name);
//System.out.println(copyNode);
copyTree(focusNode.leftChild);
copyTree(focusNode.rightChild);
}
}
}

Here's one solution. I added a toString() method to the Node class for presentation purposes.
class Node {
int key;
String name;
Node leftChild;
Node rightChild;
Node(int key, String name) {
this.key = key;
this.name = name;
}
public String toString() {
return "[" + key + "," + name + "]";
}
}
The BinaryTree was slightly modified as well:
class BinaryTree {
public Node root;
public BinaryTree copyTree(Node focusNode) {
BinaryTree bt = new BinaryTree();
bt.root = preOrderCopy(focusNode);
return bt;
}
public static void preOrderPrint(BinaryTree t) {
preOrderPrint(t.root);
}
public static void preOrderPrint(Node n) {
if (n == null) {
// base case
return;
}
System.out.println(n);
preOrderPrint(n.leftChild);
preOrderPrint(n.rightChild);
}
private Node preOrderCopy(Node focusNode) {
if (focusNode == null) {
// base case
return null;
}
Node copy = new Node(focusNode.key, focusNode.name);
copy.leftChild = preOrderCopy(focusNode.leftChild);
copy.rightChild = preOrderCopy(focusNode.rightChild);
return copy;
}
}
To test the code, I created a BinaryTree based on the one shown on the Wikipedia page for Tree Traversal. Here's a picture of the tree used in this example:
The proper Pre-order traversal for this example is : F, B, A, D, C, E, G, I, H. You can use the following code to test this implementation:
public class NodeTest {
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
Node a = new Node(1, "A");
Node b = new Node(2, "B");
Node c = new Node(3, "C");
Node d = new Node(4, "D");
Node e = new Node(5, "E");
Node f = new Node(6, "F");
Node g = new Node(7, "G");
Node h = new Node(8, "H");
Node i = new Node(9, "I");
f.leftChild = b;
b.leftChild = a;
b.rightChild = d;
d.leftChild = c;
d.rightChild = e;
f.rightChild = g;
g.rightChild = i;
i.leftChild = h;
bt.root = f;
System.out.println("Print full tree:");
BinaryTree.preOrderPrint(bt.copyTree(f));
System.out.println("Only print f's left sub-tree:");
BinaryTree.preOrderPrint(bt.copyTree(f.leftChild));
}
}
Running the above code produces the following output:
Print full tree:
[6,F]
[2,B]
[1,A]
[4,D]
[3,C]
[5,E]
[7,G]
[9,I]
[8,H]
Only print f's left sub-tree:
[2,B]
[1,A]
[4,D]
[3,C]
[5,E]

To copy from tree a to tree b. you can use two static method like me. My idea comes from adding Element method and removing Element method. they are similar.
public static Node copyRec(Node a, Node b)//copy from b to a
{
if(b!=null)
{
a=new Node(b.data);
a.leftChild=copyRec(a.leftChild,b.leftChild);
a.rightChild=copyRec(a.rightChild,b.rightChild);
return a;
}
return null;
}
public static void copy(BST a, BST b)
{
a.root=copyRec(a.root,b.root);
}

Related

Linked list: Not working as expected

I created a singly linked list , it giving below error . not sure what is wrong, ant suggestion
Error/ OP - List is javaTest.LinkedListcreation#1540e19d
I am not sure what this value in Output is meant for .
Process finished with exit code 0
public class LinkedList{
public static void main (String[] a){
LinkedListcreation L1 = new LinkedListcreation();
L1.addNodeAtEnd("1");
System.out.print("List is " + L1);
}
}
class LinkedListcreation {
int listcount;
node head;
LinkedListcreation() {
head = new node(0);
listcount=0;
}
node Temp;
void addNodeAtEnd(Object d){
node Current = head;
Temp = new node(d);
while (Current.getNext()!= null){
Current = Current.getNext();
}
Current.setNext(Temp);
listcount++;
}
}
class node {
Object data;
node next;
node(Object d) {
next = null;
this.data=d;
}
node(Object d, node nextNode) {
next = nextNode;
this.data=d;
}
public Object getdata(){
return data;
}
public void setdata(int d){
data = d;
}
public node getNext(){
return next;
}
public void setNext (node nextValue){
next = nextValue;
}
}
Your code is all right, but in order to print useful information about an object (your list in this example), you need to override the toString method in your LinkedListcreation class.
For example:
public String toString() {
return "List with " + this.listcount + " nodes.";
}
As everybody said, you have to override toString(). Here you have the right implementation:
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
sb.append(head.data.toString());
node n;
while(n = head.getNext() != null)
sb.append(", " + n.data.toString());
sb.append("]");
return sb.toString();
}
you are trying to print the list object rather than the element which you have added and more over what you see is not error. check about toString() method in java to understand the output which you see.
Modify your main() as below to see the element you added.
public static void main (String[] a){
LinkedListcreation L1 = new LinkedListcreation();
L1.addNodeAtEnd("1");
System.out.print("List is " + L1.head.next.data);
}
output : List is 1
Your code does not have any error. If you want to print the nodes in your list you just have to add another function in you LinkedListcreation class which will iterate over your list and print each node's data. Add this block in your LinkedListcreation class.
public void printList(){
node current = head.next;
while(current!=null){
System.out.println("node's data is: "+ current.getdata());
current = current.getNext();
}
}
Also in your main function call the above mentioned function using your list's object L1.
L1.printList();
The code has compiler errors. Try corrected code below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class LinkedList
{
static void Main(String[] a){
LinkedListcreation L1 = new LinkedListcreation();
L1.addNodeAtEnd("1");
Console.WriteLine("List is " + L1);
}
}
public class LinkedListcreation
{
int listcount;
node head;
public LinkedListcreation()
{
head = new node(0);
listcount = 0;
}
node Temp;
public void addNodeAtEnd(Object d)
{
node Current = head;
Temp = new node(d);
while (Current.getNext() != null)
{
Current = Current.getNext();
}
Current.setNext(Temp);
listcount++;
}
}
public class node
{
Object data;
node next;
public node(Object d)
{
next = null;
this.data = d;
}
node(Object d, node nextNode)
{
next = nextNode;
this.data = d;
}
public Object getdata()
{
return data;
}
public void setdata(int d)
{
data = d;
}
public node getNext()
{
return next;
}
public void setNext(node nextValue)
{
next = nextValue;
}
}
}
​

K-Ary Tree Implementation in Java: how to?

I've a university project about creating two classes, Tree class and Node class, to implement a k-ary tree using Java.
In the class Tree, there should be a constructor which recives as input an int that indicates the tree arity.
I've worked before with general trees and this was my result:
Class tree: *
Class node: *
I absolutely don't know where and how to start to build this project (as I don't know how to manage the arity, maybe with ArrayList?).
Any advice and suggestions will be greatly appreciated :)
Thanks in advance.
Here are the new versions of the classes, with the methods that you needed.
Node:
import java.util.ArrayList;
import java.util.List;
public class Node {
public Node parent; // The parent of the current node
public List<Node> children; // The children of the current node
public Object info;
public static int maxNrOfChildren; // Equal to the k-arity;
public Node (Object info)
{
this.info=info;
children = new ArrayList<Node>(maxNrOfChildren);
}
public void addChild(Node childNode, int position)
// You must take care so that future insertions don't override a child on i-th position
{
if(position>=maxNrOfChildren-1)
{
// Throw some error
}
else
{
System.out.println("this.children="+this.children);
if(this.children.get(position)!=null)
{
// There is alerady a child node on this position; throw some error;
}
else
{
childNode.parent=this;
this.children.set(position, childNode);
}
}
}
}
Tree:
import java.util.ArrayList;
import java.util.List;
public class Tree {
public Node root;
public Tree(int kArity)
{
Node.maxNrOfChildren=kArity;
}
public void addRoot(Object info)
{
root=new Node(info);
root.parent=null;
root.children=new ArrayList<Node>(Node.maxNrOfChildren);
}
public void addNewNodeVasithChildOfNodeU(Node u, Object info, int i)
{
Node child=new Node(info);
u.addChild(child, i);
}
// I've made the above two methods of type void, not Node, because
// I see no reason in returning anything; however, you can override by calling
//'return root;' or 'return child;'
public int numberOfNodesInTree(Node rootNode){
int count=0;
count++;
if(rootNode.children.size()!=0) {
for(Node ch : rootNode.children)
count=count+numberOfNodesInTree(ch);
}
return count;
}
public int numberOfNodesInTree()
{
return numberOfNodesInTree(this.root);
}
public void changeRoot(Node newRoot, int i)
{
Node oldRoot=this.root;
newRoot.parent=null;
newRoot.addChild(oldRoot, i);
oldRoot.parent=newRoot;
this.root=newRoot;
}
public static void main(String args[])
{
Tree tree=new Tree(3);
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
tree.addRoot("root");
tree.root.addChild(a,0);
a.addChild(b,0);
tree.root.addChild(c,1);
System.out.println(tree.numberOfNodesInTree(tree.root));
}
}
The logic is correct, but I am getting some Java-related error when I run the main method and I haven't yet figured out what the problem is.
this can be a starting point:
Node Class
import java.util.ArrayList;
import java.util.List;
public class Node {
public Node parent;//the parent of the current node
public List<Node> children = new ArrayList<Node>();//the children of the current node
public String name;//or any other property that the node should contain, like 'info'
public static int maxNrOfChildren;//equal to the k-arity;
public Node (String nodeName)
{
name=nodeName;
}
public void addChild(Node childNode)
{
if(this.children.size()>=maxNrOfChildren)
{
//do nothing (just don't add another node), or throw an error
}
else
{
childNode.parent=this;
this.children.add(childNode);
}
}
}
Tree Class
import java.util.ArrayList;
import java.util.List;
public class Tree {
public Node root = new Node("root");
public Tree(int kArity)
{
Node.maxNrOfChildren=kArity;
root.parent=null;
}
public void traverseTree(Node rootNode)//depth first
{
System.out.println(rootNode.name);
if(rootNode.children.size()!=0)
for(Node ch : rootNode.children)
traverseTree(ch);
}
public static void main(String args[])
{
Tree tree=new Tree(3);
Node a = new Node("a");
Node b = new Node("b");
Node c = new Node("c");
tree.root.addChild(a);
a.addChild(b);
tree.root.addChild(c);
tree.traverseTree(tree.root);
}
}
Please give further details about your project specifications, otherwise i can't figure out which kind of functionality you need within these classes
The idea behind creating a k-array, is that this is not a conventional structure like a list or a set, the node is like an element in a linked list, it point to the n other child node and can also point to the parent, whant determine what should be the child or the parent in that sctructure is an entire different question. As for the list of child in the node you can use any structure you whant ArrayList most likely will be a good fit. The choice of a structure depend on many factors like size, how often it will be accessed does it need to be sorted etc.
Have a look at this. Hope it helps.
import java.util.ArrayList;
public class Nary
{
public static Node root;
public static int insert(Node rootNode, int parentId, ArrayList<Node> nodeToAdd)
{
if(rootNode == null)
return 0;
if(rootNode.children == null)
rootNode.children = new ArrayList<Node>();
if(rootNode.id == parentId)
{
for(int i =0; i < nodeToAdd.size(); i++)
{
Node node = nodeToAdd.get(i);
node.parent = rootNode;
rootNode.children.add(node);
}
return 1;
}
else
{
for(int i = 0; i < rootNode.children.size(); i++)
{
int resultFlag = insert(rootNode.children.get(i), parentId, nodeToAdd);
if(resultFlag == 1)
{
return 1;
}
}
}
return -1;
}
public static void traverse(Node root)
{
if(root == null)
{
return;
}
System.out.println(root.data + " " + root.id );
for(Node child : root.children)
{
traverse(child);
}
}
public static void main(String[] args) {
// Insertion
root = new Node(0, "root");
int parentId = root.id;
Node Bread = new Node(1, "Bread");
Node Milk = new Node(2, "Milk");
Node Meat = new Node(3, "Meat");
Node Eggs = new Node(4, "Eggs");
ArrayList<Node> nodeList = new ArrayList<Node>();
nodeList.add(Bread);
nodeList.add(Milk);
nodeList.add(Meat);
nodeList.add(Eggs);
insert(root, parentId, nodeList);
// Add children for Bread
parentId = Bread.id;
Node Bread0 = new Node(11, "Whole-Wheat");
Node Bread1 = new Node(12, "Whole-Grain");
Node Bread2 = new Node(13, "Italian");
ArrayList<Node> nodeList1 = new ArrayList<Node>();
nodeList1.add(Bread0);
nodeList1.add(Bread1);
nodeList1.add(Bread2);
insert(root, parentId, nodeList1);
Add children for Milk
parentId = Milk.id;
Node Milk0 = new Node(21, "Whole");
Node Milk1 = new Node(22, "skim");
Node Milk2 = new Node(23, "Almond");
ArrayList<Node> nodeList2 = new ArrayList<Node>();
nodeList2.add(Milk0);
nodeList2.add(Milk1);
nodeList2.add(Milk2);
insert(root, parentId, nodeList2);
traverse(root);
}
}
class Node{
int id;
String data;
Node parent;
ArrayList<Node> children;
public Node(int id, String data)
{
this.id = id;
this.data = data;
}
}

add method not working for linked list in Java

I'm trying to create a method that will add a node to my linked list. The method takes a String. This is the method that I created:
public void add(String x)
{
Node newNode = new Node();
newNode.element = x;
newNode.nextNode = firstNode;
firstNode = newNode;
}
Unfortunately, this code isn't working. Is there a way I can alter it to make it work?
Here are all the information I was provided with:
Linked List Class with Node inner-class:
class LinkedList implements StringCollection
{
private static class Node
{
public String element;
public Node nextNode;
public Node (String element)
{
this.element = element;
this.nextNode = null;
}
}
private Node firstNode;
public NodeStringCollection ()
{
firstNode = null;
}
//add method goes here
public String toString ()
{
String s = "";
Node node = firstNode;
while (node != null)
{
s = s + node.element + " ";
node = node.nextNode;
}
return s;
}
}
Tested Linked Class:
Class Test
{
public static void main(String [] args)
{
StringCollection sc = new LinkedList ();
sc.add (new String ("A"));
sc.add (new String ("B"));
sc.add (new String ("C"));
sc.add (new String ("D"));
System.out.println (sc);
int countStrings = sc.size ();
System.out.println (countStrings);
}
}
The Output
D C B A
4
I fixed your code. What you did wrong is that the element that you added to the LinkedList replaced the old firstNode. So the last node that you add to your implementation would become the new first node. Therefore, your LinkedList printed D C B A which is the reverse of what it should be.
The code below stores the first node and the last node. When a new node is added, we let the last node point to the newly created node and then set the last node to the newly created node:
Code
public class LinkedList {
public static class Node {
public String element;
public Node nextNode;
public Node(String element) {
this.element = element;
this.nextNode = null;
}
}
private Node firstNode;
private Node lastNode;
public LinkedList() {
firstNode = null;
lastNode = null;
}
public void add(String x) {
Node newNode = new Node(x);
if (firstNode == null)
firstNode = newNode;
if (lastNode != null)
lastNode.nextNode = newNode;
lastNode = newNode;
}
public String toString() {
String s = "";
Node node = firstNode;
while (node != null) {
s = s + node.element + " ";
node = node.nextNode;
}
return s;
}
}
Example code
public static void main(String args[]) throws Exception {
LinkedList sc = new LinkedList();
sc.add(new String("A"));
sc.add(new String("B"));
sc.add(new String("C"));
sc.add(new String("D"));
System.out.println(sc);
}
Output
A B C D

Printing Tree Data Structure Alphabetically

I have a big task to do as an exercise for Data Structures and algorithms, and part of it is to modify this tree data structure to print the tree in an alphabetical order.I won't post the whole task because it is huge. Im stuck on the last part which asks me to modify the given tree Data Structure to print the tree in an alphabetical order. I am stuck on it for couple of days and simple don't have any idea how to do it. Any help would be appriciated, thanks. My opinion is that i have to somehow modify the printTreeRecursive() method.
For example the current data structure will print a tree like this:
c: d c b a
(The first added child is printed last).
Where c: is the root and d c b a are his children
But im supposed to modify it to look like this:
c: a b c d
Here is the data structure:
public class SLLTree<E> implements Tree<E> {
// SLLNode is the implementation of the Node interface
class SLLNode<P> implements Node<P> {
// Holds the links to the needed nodes
SLLNode<P> parent, sibling, firstChild;
// Hold the data
P element;
public SLLNode(P o) {
element = o;
parent = sibling = firstChild = null;
}
public P getElement() {
return element;
}
public void setElement(P o) {
element = o;
}
}
protected SLLNode<E> root;
public SLLTree() {
root = null;
}
public Node<E> root() {
return root;
}
public Tree.Node<E> parent(Tree.Node<E> node) {
return ((SLLNode<E>) node).parent;
}
public int childCount(Tree.Node<E> node) {
SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
int num = 0;
while (tmp != null) {
tmp = tmp.sibling;
num++;
}
return num;
}
public void makeRoot(E elem) {
root = new SLLNode<E>(elem);
}
public Node<E> addChild(Node<E> node, E elem) {
SLLNode<E> tmp = new SLLNode<E>(elem);
SLLNode<E> curr = (SLLNode<E>) node;
tmp.sibling = curr.firstChild;
curr.firstChild = tmp;
tmp.parent = curr;
return tmp;
}
public void remove(Tree.Node<E> node) {
SLLNode<E> curr = (SLLNode<E>) node;
if (curr.parent != null) {
if (curr.parent.firstChild == curr) {
// The node is the first child of its parent
// Reconnect the parent to the next sibling
curr.parent.firstChild = curr.sibling;
} else {
// The node is not the first child of its parent
// Start from the first and search the node in the sibling list
// and remove it
SLLNode<E> tmp = curr.parent.firstChild;
while (tmp.sibling != curr) {
tmp = tmp.sibling;
}
tmp.sibling = curr.sibling;
}
} else {
root = null;
}
}
class SLLTreeIterator<T> implements Iterator<T> {
SLLNode<T> start, current;
public SLLTreeIterator(SLLNode<T> node) {
start = node;
current = node;
}
public boolean hasNext() {
return (current != null);
}
public T next() throws NoSuchElementException {
if (current != null) {
SLLNode<T> tmp = current;
current = current.sibling;
return tmp.getElement();
} else {
throw new NoSuchElementException();
}
}
public void remove() {
if (current != null) {
current = current.sibling;
}
}
}
public Iterator<E> children(Tree.Node<E> node) {
return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
}
void printTreeRecursive(Node<E> node, int level) {
if (node == null)
return;
int i;
SLLNode<E> tmp;
for (i = 0; i < level; i++)
System.out.print(" ");
System.out.println(node.getElement().toString());
tmp = ((SLLNode<E>) node).firstChild;
while (tmp != null) {
printTreeRecursive(tmp, level + 1);
tmp = tmp.sibling;
}
}
public void printTree() {
printTreeRecursive(root, 0);
}
public int countMaxChildren() {
return countMaxChildrenRecursive(root);
}
int countMaxChildrenRecursive(SLLNode<E> node) {
int t = childCount(node);
SLLNode<E> tmp = node.firstChild;
while (tmp != null) {
t = Math.max(t, countMaxChildrenRecursive(tmp));
tmp = tmp.sibling;
}
return t;
}
}
public interface Tree<E> {
// //////////Accessors ////////////
public Tree.Node<E> root();
public Tree.Node<E> parent(Tree.Node<E> node);
public int childCount(Tree.Node<E> node);
// //////////Transformers ////////////
public void makeRoot(E elem);
public Tree.Node<E> addChild(Tree.Node<E> node, E elem);
public void remove(Tree.Node<E> node);
// //////////Iterator ////////////
public Iterator<E> children(Tree.Node<E> node);
// //////Inner interface for tree nodes ////////
public interface Node<E> {
public E getElement();
public void setElement(E elem);
}
}
public class SLLTreeTest {
public static void main(String[] args) {
Tree.Node<String> a, b, c, d;
SLLTree<String> t = new SLLTree<String>();
t.makeRoot("C:");
a = t.addChild(t.root, "Program files");
b = t.addChild(a, "CodeBlocks");
c = t.addChild(b, "codeblocks.dll");
c = t.addChild(b, "codeblocks.exe");
b = t.addChild(a, "Nodepad++");
c = t.addChild(b, "langs.xml");
d = c;
c = t.addChild(b, "readme.txt");
c = t.addChild(b, "notepad++.exe");
a = t.addChild(t.root, "Users");
b = t.addChild(a, "Darko");
c = t.addChild(b, "Desktop");
c = t.addChild(b, "Downloads");
c = t.addChild(b, "My Documents");
c = t.addChild(b, "My Pictures");
b = t.addChild(a, "Public");
a = t.addChild(t.root, "Windows");
b = t.addChild(a, "Media");
t.printTree();
t.remove(d);
t.printTree();
System.out.println("The maximum number of children is "
+ t.countMaxChildren());
}
}
As I see, my initial suggestion is good-enough for the asker and other commenters as well. So, as this is a studying task, I will not write code as an answer (I would take all the fun, wouldn't I?). I will share some important checkpoints to reach in the thought process, which, if reached should lead to the solution:
we need a Collection
we need to use a breadth-first traversing (printTreeRecursive is a good example)
we need to look at the while cycle of printTreeRecursive, as it is key to reach a traversing
whenever we reach a node, we should insert sort the node into the collection
after the traversing, we iterate the Collection and print out its elements

Class cast exception while adding a second tree node to PriorityQueue

I am trying to print a binary tree by BFS.
my implementation is with a PriorityQueue.
in the beginning i insert root into PriorityQueue.
then in loop, i pull a node from PriorityQueue, print it, and insert his childs(if thay are not null) into PriorityQueue.
why when inserting the second node, i get this exception:
Exception in thread "main" java.lang.ClassCastException: Node cannot be cast to java.lang.Comparable
this is my code:
class main:
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Tree tree = new Tree();
}
}
class Node:
public class Node {
public Node(){}
public Node(int num)
{
value = num;
}
private int value;
private Node left;
private Node right;
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
}
class tree:
public class Tree {
private Node root;
public Tree()
{
root = new Node(5);
Node node2 = new Node(2);
Node node10 = new Node(10);
Node node8 = new Node(8);
Node node6 = new Node(6);
Node node15 = new Node(15);
root.setRight(node10);
root.setLeft(node2);
node10.setRight(node15);
node10.setLeft(node8);
node8.setLeft(node6);
printToWidth(root);
}
public void printToWidth(Node node)
{
PriorityQueue<Node> queue = new PriorityQueue<Node>();
queue.add(node);
while( !(queue.isEmpty()))
{
Node n = queue.poll();
System.out.println(n.getValue());
if (n.getLeft() != null)
queue.add(n.getLeft());
if (n.getRight() != null)
queue.add(n.getRight());
}
System.out.println("end printToWidth");
}
}
You've got two options:
Make Node implement Comparable<Node>, so that the elements can be inserted according to their natural ordering. This is likely the easier of the two.
public int compareTo(Node other) {
return value - other.getValue();
}
Use a custom Comparator<Node> and supply a compare method there, with an initial capacity.
PriorityQueue<Node> queue = new PriorityQueue<Node>(10, new Comparator<Node>() {
public int compare(Node left, Node right) {
return left.getValue() - other.getValue();
}
});
The exception is telling you, make Node implement Comparable<Node>.
You can insert the first node because it has nothing to compare to, so the comparison is not needed.

Categories