Implement maketree setleft and setright for right inthreaded

Implement maketree, setleft, and setright for right in-threaded binary trees using the sequential array representation.

Solution

import java.util.Scanner; class node{ int info; node left; node right; boolean rthread; } public class ThreadedBinaryTrees { static node maketree(int x) { node p; p = getnode(); p.info = x; p.left = null; p.right = null; p.rthread = true; return p; } static node getnode() { return new node(); } static void setleft(node p, int x) { node q; if (p == null) { System.out.println(\"Void insertion\ \"); } else if (p.left != null) { System.out.println(\"Invalid insertion\ \"); } else { q = getnode(); q.info = x; p.left = q; q.left = null; q.right = p; q.rthread = true; } }/*end setleft*/ static void setright(node p, int x) { node q, r; if (p == null) { System.out.println(\"Void insertion\ \"); } else if (!p.rthread) { System.out.println(\"Invalid insertion\ \"); } else { q = getnode(); q.info = x; /* save the inorder successor of node(p) */ r = p.right; p.right = q; p.rthread = false; q.left = null; /*the inorder successor of node(q) is the previous suuccessor of node(p)*/ q.right = r; q.rthread = true; } /* end else */ } /* end setright */ static void intraversal(node tree) { node p, q; p = tree; do { q = null; while (p != null) { /* Traverse left branch */ q = p; p = p.left; }/*end while*/ if (q != null) { System.out.print(q.info + \" \"); p = q.right; while (q.rthread && p != null) { System.out.print(p.info + \" \"); q = p; p = p.right; }/* end while */ }/* end if */ } while (q != null); } public static void main(String[] args){ node ptree; node p = null, q; int number; System.out.println(\"Binary Tree Traversal\ \ \"); System.out.println(\"Enter numbers :\"); Scanner scan = new Scanner(System.in); number = scan.nextInt(); ptree = maketree(number); while((number=scan.nextInt())!=-999){ p = q = ptree; while(q!=null){ p=q; if(number=p.info){ if(!p.rthread){ q = p.right; } if(p.rthread){ setright(p, number); break; } } } if (number
Implement maketree, setleft, and setright for right in-threaded binary trees using the sequential array representation.Solution import java.util.Scanner; class

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site