How to print binary search tree in nice diagram? - java

I have to implement binary search tree with method that prints nice diagram with connections like this:
For now I managed to print this:
However I'm struggling to make it better :/
Do you have any hints how to fix that?
It's my code of instance implementing it:
public interface PrintableTree {
class Node {
int data;
Node left, right;
Node(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class Trunk {
Trunk prev;
String str;
Trunk(Trunk prev, String str) {
this.prev = prev;
this.str = str;
}
}
Node insert_Recursive(Node root, int key);
void add(int i);
String prettyPrint();
static PrintableTree getInstance() {
return new PrintableTree() {
String stringOfTree = "";
static final int COUNT = 2;
Node root;
#Override
public void add(int i) {
root = insert_Recursive(root, i);
}
#Override
public Node insert_Recursive(Node root, int key) {
if (root == null) {
root = new Node(key);
return root;
}
if (key < root.data)
root.left = insert_Recursive(root.left, key);
else if (key > root.data)
root.right = insert_Recursive(root.right, key);
return root;
}
#Override
public String prettyPrint() {
printTree(root, null, false);
return "";
}
public void showTrunks(Trunk p) {
if (p == null) {
return;
}
showTrunks(p.prev);
System.out.print(p.str);
}
public void printTree(Node root, Trunk prev, boolean isLeft) {
if (root == null) {
return;
}
String prev_str = " ";
Trunk trunk = new Trunk(prev, prev_str);
printTree(root.left, trunk, true);
if (prev == null) {
trunk.str = "";
} else if (isLeft) {
trunk.str = "┌";
prev_str = " │";
} else {
trunk.str = "└";
prev.str = prev_str;
}
showTrunks(trunk);
System.out.println(" " + root.data);
if (prev != null) {
prev.str = prev_str;
}
trunk.str = " │";
printTree(root.right, trunk, false);
}
};
}
}

You could use these functions. They return a string, so it is up to the caller to print it.
I also find it nicer when the right subtree is printed upwards, and the left subtree downwards. That way, the tree is just rotated 90° from how it is usually depicted -- with the root at the top.
Here is the relevant code:
public String pretty() {
return pretty(root, "", 1);
}
private String pretty(Node root, String prefix, int dir) {
if (root == null) {
return "";
}
String space = " ".repeat(("" + root.data).length());
return pretty(root.right, prefix + "│ ".charAt(dir) + space, 2)
+ prefix + "└ ┌".charAt(dir) + root.data
+ " ┘┐┤".charAt((root.left != null ? 2 : 0)
+ (root.right != null ? 1 : 0)) + "\n"
+ pretty(root.left, prefix + " │".charAt(dir) + space, 0);
}

Related

How to remove a string from a Binary Search Tree

I am trying to remove a string from a BST and I cannot figure it out. I know how to remove integers but cannot convert my code to remove Nodes that have strings. Here is the code I currently have
/*
* Removes the specified string from the BST
*/
public boolean remove(String s){
Node ans = remove(root, s);
if(ans == null){
return false;
} else {
return true;
}
}
private static Node remove(Node root, String s) {
if (root == null)
return null;
if (s.compareTo(root.data) < 0) {
root.left = remove(root.left, s);
} else if (s.compareTo(root.data) > 0) {
root.right = remove(root.right, s);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null)
return root.left;
root.data = minimum(root.right);
root.right = remove(root.right, root.data);
}
return root;
}
public static String minimum(Node root) {
String minimum = root.data;
while (root.left != null) {
minimum = root.left.data;
root = root.left;
}
return minimum;
}
Replace all "Integer" with "String" .
I know this was 2 years ago but I case someone needs help I did this instead..
private String minimum(BSTNode treeRoot) {
String minimum = treeRoot.value;
while (treeRoot.left != null) {
minimum = treeRoot.left.value;
treeRoot = treeRoot.left;
}
return minimum;
}
private BSTNode removeValue(BSTNode treeRoot, String s){
if(treeRoot == null){
return treeRoot;
} else if(s.compareTo(treeRoot.value) < 0){
treeRoot.left = removeValue(treeRoot.left, s);
} else if(s.compareTo(treeRoot.value) > 0){
treeRoot.right = removeValue(treeRoot.right, s);
} else {
if(treeRoot.left == null){
return treeRoot.right;
} else if(treeRoot.right == null){
return treeRoot.left;
}
treeRoot.value = minimum(treeRoot.right);
treeRoot.right = removeValue(treeRoot.right, treeRoot.value);
}
return treeRoot;
}

recursion not navigating all child nodes

I need to navigate my 23Tree and print all the levels and corresponding elements. However, my recursion goes in any one direction and does not return and perform other calls. Any help would be much appreciated.
Here is my node class:
class Node<T extends Comparable<T>> {
List<T> vals = new ArrayList<T>();
List<Node<T>> children = new ArrayList<Node<T>>();
boolean isLeaf() { return children.size() == 0; }
boolean is4Node() { return vals.size() == 3; }
// new Nodes always are 2-nodes (1 value). The node may be
// a leaf, or has 2 children.
Node(T x) {
vals.add(x);
}
Node(T x, Node<T> left, Node<T> right) {
vals.add(x);
children.add(left);
children.add(right);
children.add(null); // hack
}
This is my recursive function to print the nodes:
private boolean iterateChildrenAndPrintPerLevelAndLevelType(int level, String levelType, Node root){
System.out.println("present element: " + root);
if(root.vals.size() == 1 ){
System.out.println("Level = " + level + " [" + levelType + "] value = " + root.vals.get(0));
}else if(root.vals.size() == 2 ){
System.out.println("Level = " + level + " [" + levelType + "] value = " + root.vals.get(0) + "/" + root.vals.get(1));
}
if(root.children.get(0) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "left", (Node) root.children.get(0));
}
if(root.children.get(1) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "middle", (Node) root.children.get(1));
}
if(root.children.get(2) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "right", (Node) root.children.get(2));
}
return true;
}
And here is the output:
present element: [[[a]b[c]]d, [[e]f[g]]h[[i]j, [k]y[z]]]
Level = 0 [root] value = d/h
present element: [[a]b[c]]
Level = 1 [left] value = b
present element: [a]
Level = 2 [left] value = a
(Edit) Here is my main method:
public static void main(String[] args) {
StringTwoThreeTree set = new StringTwoThreeTree();
try{
String line = null;
FileReader fileReader =
new FileReader("C:\\Users\\Redoubt\\IdeaProjects\\23Tree\\src\\resources\\test.dat");
// new FileReader("C:\\Users\\Redoubt\\IdeaProjects\\23Tree\\src\\resources\\a4q1search.txt");
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
set.insert(line);
}
// System.out.println("\n\n\n");
String str = set.toString();
// System.out.println(str);
set.print();
}catch (Exception e){
}
}
and the contents of the file test.dat :
a
a
b
c
d
e
f
g
h
i
j
k
y
z
Just for clarity, I'm adding the 2 large classes as well:
StringTwoThree class:
import java.util.List;
public class StringTwoThreeTree extends TwoThreeTree<String> {
#Override
public String toString() {
return super.toString();
}
public void insert(String str){
super.add(str);
}
public void print(){
Node root = super.root;
// System.out.println(root.vals);
// dumpList("",root);
iterateChildrenAndPrintPerLevelAndLevelType(0, "root", root);
super.displayLevelWise();
}
private void dumpList(String string, Node list) {
int i = 0;
for (Object item : list.children) {
if (item instanceof List) {
dumpList(string + i, (Node) item);
} else {
System.out.println(String.format("%s%d %s", string, i, item));
}
++i;
}
}
private boolean iterateChildrenAndPrintPerLevelAndLevelType(int level, String levelType, Node root){
System.out.println("present element: " + root);
if(root.vals.size() == 1 ){
System.out.println("Level = " + level + " [" + levelType + "] value = " + root.vals.get(0));
}else if(root.vals.size() == 2 ){
System.out.println("Level = " + level + " [" + levelType + "] value = " + root.vals.get(0) + "/" + root.vals.get(1));
}
if(root.children.get(0) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "left", (Node) root.children.get(0));
}
if(root.children.get(1) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "middle", (Node) root.children.get(1));
}
if(root.children.get(2) != null){
iterateChildrenAndPrintPerLevelAndLevelType(level+1, "right", (Node) root.children.get(2));
}
return true;
}
}
TwoThreeTree class:
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Queue;
public class TwoThreeTree<T extends Comparable<T>> implements Iterable<T> {
// a Node has 1 (2-Node) or 2 (3-Node) values
// and 2 or 3 children. Values and children are stored
// in ArrayLists. If there are children, that ArrayList
// has a null element at the end, so as to make easier the
// method which adds a new child.
class Node<T extends Comparable<T>> {
List<T> vals = new ArrayList<T>();
List<Node<T>> children = new ArrayList<Node<T>>();
boolean isLeaf() { return children.size() == 0; }
boolean is4Node() { return vals.size() == 3; }
// new Nodes always are 2-nodes (1 value). The node may be
// a leaf, or has 2 children.
Node(T x) {
vals.add(x);
}
Node(T x, Node<T> left, Node<T> right) {
vals.add(x);
children.add(left);
children.add(right);
children.add(null); // hack
}
public String toString() {
String answer = "[";
for (int i=0; i<vals.size(); i++) {
if (i != 0) answer += ", ";
if (children.size() != 0)
answer += children.get(i).toString();
answer += vals.get(i);
}
if (children.size() != 0)
answer += children.get(children.size()-2).toString();
return answer + "]";
}
// used in Iterator
void getVals(List<T> iteratorList) {
for (int i=0; i<vals.size(); i++) {
if (children.size() != 0)
children.get(i).getVals(iteratorList);
iteratorList.add(vals.get(i));
}
if (children.size() != 0)
children.get(children.size()-2).getVals(iteratorList);
}
// recursively adds a new value to a subtree
boolean add(T val) {
if (isLeaf())
return addToLeaf(val);
else return addToInterior(val);
}
// new values are always added to a leaf. The result may be a 4-node leaf.
boolean addToLeaf(T x) {
int cmp;
// size is 1 for a 2-node, or 2 for a 3-node
for (int i = 0; i < vals.size(); i++) {
cmp = x.compareTo(vals.get(i));
if (cmp == 0) return false;
else if (cmp < 0) {
vals.add(i,x);
return true;
}
}
vals.add(x);
return true;
}
// adds a value to a subtree rooted by an interior node. If
// the addition results in one of the children being a 4-node,
// then adjustments are made.
boolean addToInterior(T x) {
int cmp;
// size is 1 for a 2-node, or 2 for a 3-node
for (int i = 0; i <= vals.size(); i++) {
if (i == vals.size()) cmp = -1; // hack because there is no vals[2]
else cmp = x.compareTo(vals.get(i));
if (cmp == 0) return false;
else if (cmp < 0) {
boolean retVal = children.get(i).add(x);
if (children.get(i).is4Node())
childIs4Node(i);
return retVal;
}
}
return false; // unreachable -- just for compiler
}
// the ith child is a 4-node
void childIs4Node(int i) {
Node<T> the4Node = children.get(i);
// move the middle value from the 4-node child up
// to its parent
if (i == 2)
vals.add(children.get(i).vals.get(1));
else vals.add(i, children.get(i).vals.get(1));
Node<T> newChild1, newChild2;
if (children.get(i).isLeaf()) {
newChild1 = new Node<T>(children.get(i).vals.get(0));
newChild2 = new Node<T>(children.get(i).vals.get(2));
}
else {
newChild1 = new Node<T>(children.get(i).vals.get(0),
children.get(i).children.get(0),
children.get(i).children.get(1));
newChild2 = new Node<T>(children.get(i).vals.get(2),
children.get(i).children.get(2),
children.get(i).children.get(3));
}
children.remove(the4Node);
children.add(i, newChild2);
children.add(i, newChild1);
}
}
Node<T> root;
public TwoThreeTree() {
root = null;
}
// TwoThreeTree add
public boolean add(T val) {
if (root == null) {
root = new Node<T>(val);
return true;
}
else {
boolean isNew = root.add(val);
// if root is a 4-node, split it
if (root.vals.size() == 3) {
Node<T> left, right;
if (root.isLeaf()) {
left = new Node<T>(root.vals.get(0));
right = new Node<T>(root.vals.get(2));
}
else {
left = new Node<T>(root.vals.get(0),
root.children.get(0),
root.children.get(1));
right = new Node<T>(root.vals.get(2),
root.children.get(2),
root.children.get(3));
}
root = new Node<T>(root.vals.get(1), left, right);
}
return isNew;
}
}
// this method creates a list containing all of the values in
// the tree and returns that list's iterator
public Iterator<T> iterator() {
List<T> vals = new ArrayList<T>();
if (root != null) root.getVals(vals);
return vals.iterator();
}
public String toString(){
String result = "[";
for (T item : this
) {
result += item.toString() + ",";
}
result = result.substring(0,result.length()-1);
result += "]";
return result;
}
public void displayLevelWise(){
}
}
Your index is going out of bound in your private boolean iterateChildrenAndPrintPerLevelAndLevelType(int level, String levelType, Node root) method.

Printing Java Binary search tree

How to print out a binary search tree in java? I have written the code to insert to the tree but without being able to print the tree i am insure if the elements are adding. I will post my code below.
public class TreeNode {
TreeNode left;
TreeNode right;
TreeNode root;
int data;
public TreeNode(int d) {
data = d;
left = right = null;
root = null;
}
public synchronized void insert(int d) {
if (root == null){
root = new TreeNode( d );
}
if (d < data) {
if (left == null) {
left = new TreeNode(d);
} else {
left.insert(d);
}
} else if (d > data) {
if (right == null) {
right = new TreeNode(d);
} else {
right.insert(d);
}
}
}
public TreeNode treeSearch(TreeNode root, int target) {
if (root != null) {
if (target < root.data) {
root = treeSearch(root.left, target);
} else if (target > root.data) {
root = treeSearch(root.right, target);
}
}
return root;
}
}
You may use following method:
void printTree(TreeNode node, String prefix)
{
if(node == null) return;
System.out.println(prefix + " + " + node.data);
printTree(node.left , prefix + " ");
printTree(node.right , prefix + " ");
}
Initial call should be printTree( root,""); from where you want to print the tree. Here root is reference of root node.
UPDATED:
You can see this code working here
you can print the tree data like this.
public void printTree(TreeNode root) {
if (root == null) return;
System.out.println(root.data);
printTree(root.left);
printTree(root.right);
}
private void print(PrintWriter out) {
print(root, out, "");
}
private void print(TreeNode subtree, PrintWriter out, String indent) {
if (subtree != null) {
print(subtree.left, out, indent + "--");
out.printf("%s %s%n", indent, subtree.data);
print(subtree.right, out, indent + "--");
}
}
Infix order: Left - root - right
public String printInfix(TreeNode root) {//prints all the words in the tree in infix order
if(root==null) {
return "";
}
return printAll(root.left+" "+root.data+" "+printAll(root.right);
}

Elements getting automatically deleted in a Binary Search Tree

I wrote the following codes for implementation of Binary Search Tree in JAVA. Everything seems to be working fine except the Search Method. Each Node has a key (Item) ,an Object of type string and a reference to left and right node.
class BinarySearchTree1
{
class Node // Node for BST
{
private int item;
private String obj;
private Node left;
private Node right;
Node(int Item,String Obj)
{
item = Item;
obj = Obj;
left = null;
right = null;
}
}
Node root; //Root of the BST
BinarySearchTree1() // Constructor
{
root = null;
}
void insert(int key,String obj) // Insertion
{
root = insertItem(root,key,obj);
}
Node insertItem(Node root,int key,String obj)
{
if(root == null)
{
root = new Node(key,obj);
return root;
}
else if(key>root.item)
root.right = insertItem(root.right,key,obj);
else
root.left = insertItem(root.left,key,obj);
return root;
}
void inOrder() // View Records in order
{
System.out.println("List: ");
inOrderRec(root);
}
void inOrderRec(Node root)
{
if(root != null)
{
inOrderRec(root.left);
System.out.println(root.item + " "+ root.obj);
inOrderRec(root.right);
}
}
void search(int key) // Search
{
Node Temp;
Temp = root;
Temp = searchRec(Temp,key);
if(Temp == null) // Element Not Found
{
System.out.println("Object for "+key+" NOT FOUND");
return;
}
System.out.println("Object for "+ Temp.item+" is "+ Temp.obj); // Element Found
}
Node searchRec(Node Temp,int key)
{
if(Temp != null)
{
if(key>Temp.item)
{
Temp.right = searchRec(Temp.right,key);
return Temp.right;
}
if(key<Temp.item)
{
Temp.left = searchRec(Temp.left,key);
return Temp.left;
}
if(key==Temp.item)
return Temp;
}
return Temp;
}
public static void main(String[] args)
{
BinarySearchTree1 b1 = new BinarySearchTree1();
b1.insert(6,"a");
b1.insert(7,"aa");
b1.insert(4,"aaa");
b1.insert(1,"aaaa");
b1.insert(9,"b");
b1.insert(8,"bb");
b1.inOrder();
b1.search(9);
b1.search(1);
b1.inOrder();
b1.search(8);
b1.search(4);
//System.out.println(b1.root.obj);
}
}
The following code outputs:
List:
1 aaaa
4 aaa
6 a
7 aa
8 bb
9 b
Object for 9 is b
Object for 1 is aaaa
List:
1 aaaa
6 a
8 bb
9 b
Object for 8 is bb
Object for 4 NOT FOUND\
Its clear that the elements with keys 4 and 7 aren't there anymore. Can anyone explain this?
It should be:
Node searchRec(Node Temp, int key) {
if (Temp != null) {
Node t = null;
if (key > Temp.item) {
t = searchRec(Temp.right, key);
return t;
}
if (key < Temp.item) {
t = searchRec(Temp.left, key);
return t;
}
if (key == Temp.item)
return Temp;
}
return Temp;
}
you were updating nodes in this methods which will break node links.
Temp.right = searchRec(Temp.right, key); // wrong
Temp.left = searchRec(Temp.left, key); // wrong
Update:
You can replace:
t = searchRec(Temp.right, key);
return t;
with this also
return searchRec(Temp.right,key);
same way with left and this will not require any temporary variable.
Node searchRec(Node Temp,int key)
{
if(Temp != null)
{
if(key>Temp.item)
{
return searchRec(Temp.right,key);
}
if(key<Temp.item)
{
return searchRec(Temp.left,key);
}
if(key==Temp.item)
return Temp;
}
return Temp;
}

How do I print my linked list?

If you look at my toString() method below, you can see that I have not finished it. I don't know how? Can anyone explain how I might proceed? Confused :-| I'm new to linked lists and so I automatically went for the Arrays.toString() approach before I realized that there is, of course, no arrays anywhere.
import java.util.Arrays;
public class LinkedIntegerStack {
private Node top = null;
private int size = 0;
public int size(){
return size;
}
public boolean isEmpty() {
if(top == null){
return true;
}
else{
return false;
}
}
public void push(int value) {
Node n = new Node(value);
n.next = top;
top = n;
size++;
}
public int top(){ //just returns a value..doesn't change structure
return top.element;
}
public int pop(){
if (isEmpty()){
throw new StackEmptyException();
}
int toReturn = top.element;
top = top.next;
size--;
return toReturn;
}
public String toString() {
return "[ Top = " + size +"]" + "[Stack = " );
}
private class Node {
int element;
Node next;
public Node(int value){
element = value;
}
}
public static void main(String[] args) throws StackEmptyException{
LinkedIntegerStack stack = new LinkedIntegerStack();
stack.push(17);
System.out.println(stack);
stack.push(11);
System.out.println(stack);
try{
stack.pop();
System.out.println(stack);
}
catch(StackEmptyException ex){
System.out.print("Stack is Empty: Error");
}
System.out.println("Stack: " + stack);
}
}
The solution is pretty easy.
It should be enough to iterate through the stack.
public String toString() {
String result = "[ Top = " + size +"]" + "[Stack = [";
if (top == null) {
return result + "]]";
Node temp = top;
while (temp != null) {
result += temp + ', '
temp = temp.next;
}
return result += temp.element + "]]";
}
Of course you should add at least getter methods to Node class, i.e. getElement() and getNext();
PS: the code isn't tested, but it should be fine.
System.out.println(Arrays.toString(stack.toArray()));
From https://stackoverflow.com/a/395403/2736496
JavaDoc: Arrays.toString(o[]), Collection.toArray(cll)

Categories

Resources