-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTreeFromString.java
147 lines (140 loc) · 4.74 KB
/
BinaryTreeFromString.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
/*
Solved by: Nagendra Singh (https://github.com/Nagendra1421)
*/
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
Scanner inp = new Scanner(System.in);
Tree theTree = new Tree();
while(true)
{
System.out.print("Enter first letter of show, ");
System.out.print("insert, find, delete, or traverse: ");
int choice = getChar();
switch(choice)
{
case 's':
theTree.displayTree();
break;
case 'i':
System.out.println("Type the characters you would like to insert");
String in = inp.next();
char [] cArray1 = in.toCharArray();
for (int i = 0; i < cArray1.length; i++)
{
theTree.root=theTree.insert(cArray1[i], theTree.root);
}
break;
default:
System.out.print("Invalid entry\n");
}
}
}
public static String getString() throws IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String s = br.readLine();
return s;
}
public static char getChar() throws IOException
{
String s = getString();
return s.charAt(0);
}
public static int getInt() throws IOException
{
String s = getString();
return Integer.parseInt(s);
}
}
class Node {
public char sData;
public Node leftChild;
public Node rightChild;
public void displayNode()
{
System.out.print('{');
System.out.print(sData);
System.out.print(", ");
System.out.print("} ");
}
}
class Node {
public char sData;
public Node leftChild;
public Node rightChild;
public void displayNode()
{
System.out.print('{');
System.out.print(sData);
System.out.print(", ");
System.out.print("} ");
}
}
class Tree {
public Node root;
public Tree()
{
root = null;
}
public Node insert(char character,Node root) {
if(root==null){
Node newNode = new Node();
Node newRootNode = new Node();
newRootNode.sData = '+';
root = newRootNode ;
root.leftChild = newNode;
root.leftChild.sData = character;
return root;
}else{
if(character < root.leftChild.sData){
root.leftChild = insert(character,root.leftChild);
return root;
}else{
root.rightChild = insert(character,root.rightChild);
return root;
}
}
}
public void displayTree() {
Stack globalStack = new Stack();
globalStack.push(root);
int nBlanks = 32;
boolean isRowEmpty = false;
System.out.println("......................................................");
while (isRowEmpty == false) {
Stack localStack = new Stack();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++) {
System.out.print(' ');
}
while (globalStack.isEmpty() == false) {
Node temp = (Node) globalStack.pop();
if (temp != null) {
System.out.print(temp.sData);
localStack.push(temp.leftChild);
localStack.push(temp.rightChild);
if (temp.leftChild != null
|| temp.rightChild != null) {
isRowEmpty = false;
}
} else {
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 2; j++) {
System.out.print(' ');
}
}
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false) {
globalStack.push(localStack.pop());
}
}
System.out.println("......................................................");
}
}