Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4fcc8c

Browse files
Threaded tree in Java
1 parent 3605a11 commit c4fcc8c

File tree

3 files changed

+290
-0
lines changed

3 files changed

+290
-0
lines changed

‎Trees/threadedtree/Demo.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package threadedtree;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo
11+
{
12+
public static void main(String[] args)
13+
{
14+
ThreadedBinaryTree tree = new ThreadedBinaryTree();
15+
int choice,x;
16+
17+
Scanner scan = new Scanner(System.in);
18+
19+
while(true)
20+
{
21+
System.out.println("1.Insert a new node");
22+
System.out.println("2.Delete a node");
23+
System.out.println("3.Inorder Traversal");
24+
System.out.println("4.Exit");
25+
26+
System.out.print("Enter your choice : ");
27+
choice = scan.nextInt();
28+
29+
if(choice == 4)
30+
break;
31+
32+
switch( choice )
33+
{
34+
case 1:
35+
System.out.print("Enter the key to be inserted : ");
36+
x = scan.nextInt();
37+
tree.insert(x);
38+
break;
39+
case 2:
40+
System.out.print("Enter the key to be deleted : ");
41+
x = scan.nextInt();
42+
tree.delete(x);
43+
break;
44+
case 3:
45+
tree.inorder();
46+
break;
47+
}
48+
}
49+
scan.close();
50+
}
51+
}

‎Trees/threadedtree/Node.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package threadedtree;
7+
8+
public class Node
9+
{
10+
public Node left;
11+
public boolean leftThread;
12+
public int info;
13+
public boolean rightThread;
14+
public Node right;
15+
16+
public Node(int i)
17+
{
18+
info = i;
19+
left = null;
20+
right = null;
21+
leftThread = true;
22+
rightThread = true;
23+
}
24+
}
25+
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package threadedtree;
7+
8+
public class ThreadedBinaryTree
9+
{
10+
private Node root;
11+
12+
public ThreadedBinaryTree()
13+
{
14+
root = null;
15+
}
16+
17+
public boolean isEmpty()
18+
{
19+
return (root == null);
20+
}
21+
22+
private Node inorderPredecessor(Node p)
23+
{
24+
if(p.leftThread == true)
25+
return p.left;
26+
else
27+
{
28+
p=p.left;
29+
while(p.rightThread == false)
30+
p=p.right;
31+
return p;
32+
}
33+
}
34+
35+
private Node inorderSuccessor(Node p)
36+
{
37+
if(p.rightThread == true)
38+
return p.right;
39+
else
40+
{
41+
p=p.right;
42+
while(p.leftThread==false)
43+
p=p.left;
44+
return p;
45+
}
46+
}
47+
48+
public void inorder()
49+
{
50+
if(root == null )
51+
{
52+
System.out.print("Tree is empty");
53+
return;
54+
}
55+
56+
/*Find the leftmost node of the tree*/
57+
Node p = root;
58+
while( p.leftThread == false )
59+
p = p.left;
60+
61+
while( p!=null )
62+
{
63+
System.out.print(p.info + " ");
64+
if (p.rightThread == true)
65+
p = p.right;
66+
else
67+
{
68+
p = p.right;
69+
while (p.leftThread == false)
70+
p = p.left;
71+
}
72+
}
73+
System.out.println();
74+
}
75+
76+
public void insert(int x)
77+
{
78+
Node p = root;
79+
Node par = null;
80+
81+
while (p!=null)
82+
{
83+
par = p;
84+
if (x < p.info)
85+
{
86+
if (p.leftThread == false)
87+
p = p.left;
88+
else
89+
break;
90+
}
91+
else if(x > p.info)
92+
{
93+
if (p.rightThread == false)
94+
p = p.right;
95+
else
96+
break;
97+
}
98+
else
99+
{
100+
System.out.println(x + " already present in the tree");
101+
return;
102+
}
103+
}
104+
105+
Node temp = new Node(x);
106+
if (par == null)
107+
{
108+
root = temp;
109+
}
110+
else if (x < par.info) /*inserted as left child*/
111+
{
112+
temp.left = par.left;
113+
temp.right = par;
114+
par.leftThread = false;
115+
par.left = temp;
116+
}
117+
else /*inserted as right child*/
118+
{
119+
temp.left = par;
120+
temp.right = par.right;
121+
par.rightThread = false;
122+
par.right = temp;
123+
}
124+
}
125+
126+
public void delete(int x)
127+
{
128+
Node p = root;
129+
Node par = null;
130+
131+
while (p != null)
132+
{
133+
if (x == p.info)
134+
break;
135+
par = p;
136+
if (x < p.info)
137+
{
138+
if (p.leftThread == false)
139+
p = p.left;
140+
else
141+
break;
142+
}
143+
else
144+
{
145+
if (p.rightThread == false)
146+
p = p.right;
147+
else
148+
break;
149+
}
150+
}
151+
152+
if (p == null || p.info != x)
153+
{
154+
System.out.println(x + " not found");
155+
return;
156+
}
157+
158+
if (p.leftThread == false && p.rightThread == false)/*Case C: 2 children*/
159+
{
160+
/*Find inorder successor and its parent*/
161+
Node ps = p;
162+
Node s = p.right;
163+
164+
while (s.leftThread == false)
165+
{
166+
ps = s;
167+
s = s.left;
168+
}
169+
p.info = s.info;
170+
p = s;
171+
par = ps;
172+
}
173+
174+
/*Case A : No child*/
175+
if (p.leftThread == true && p.rightThread == true)
176+
{
177+
if (par == null)
178+
root = null;
179+
else if (p == par.left)
180+
{
181+
par.leftThread = true;
182+
par.left = p.left;
183+
}
184+
else
185+
{
186+
par.rightThread = true;
187+
par.right = p.right;
188+
}
189+
return;
190+
}
191+
192+
/*Case B : 1 child*/
193+
Node ch;
194+
if (p.leftThread == false) /*node to be deleted has left child */
195+
ch = p.left;
196+
else /*node to be deleted has right child */
197+
ch = p.right;
198+
199+
if (par == null) /*node to be deleted is root node*/
200+
root = ch;
201+
else if (p == par.left)/*node is left child of its parent*/
202+
par.left = ch;
203+
else /*node is right child of its parent*/
204+
par.right = ch;
205+
206+
Node pred = inorderPredecessor(p);
207+
Node succ = inorderSuccessor(p);
208+
209+
if (p.leftThread == false) /*if p has left child, right is a thread */
210+
pred.right = succ;
211+
else /*p has right child,left is a thread*/
212+
succ.left = pred;
213+
}
214+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /