Question

In: Computer Science

Design algorithms for the following operations for a binary tree T: . preorderNext(p) : Return the...

Design algorithms for the following operations for a binary tree T:

. preorderNext(p) : Return the position visited after p in a preorder traversal of T, or NULL if p is the last node visited.
. inorderNext(p) : Return the position visited after p in an inorder traversal of T, or NULL if p is the last node visited.
. postorderNext(p) : Return the position visited after p in a postorder traversal of T, or NULL if p is the last node visited.

Solutions

Expert Solution

ANSWER :

CODE IN JAVA:
NOTE: Add the below methods to either LinkedBinaryTree or AbstractBinaryTree class.

//The following preorderNext method accepts a position p and then returns a
//position visited after the position p in a preorder traversal of a binary
//tree T.
//In the preorder traversal, get the current position, traverse the left
//subtree, and traverse the right subtree.
public Position<E> preorderNext(Position<E> p)
{
   if(left(p) != null)
        return left(p);

   if(right(p) != null)
        return right(p);

   Position<E> k = parent(p);
   while(k != null && (right(k) == null || right(k) == p))
   {
        p = k;
        k = parent(k);
   }

   if(k == null)
        return null;

   return right(k);
} // end of preorderNext method

TEST PROGRAM:

File: LinkedBinaryTreeDemo.java

// LinkedBinaryTreeDemo class implementation
public class LinkedBinaryTreeDemo
{
   // start main method
   public static void main(String[] args)
   {
       // create an object for LinkedBinaryTree class
       LinkedBinaryTree<Integer> T = new LinkedBinaryTree<Integer>();
    
       // add several elements to the binary tree T
       Position<Integer> p1 = T.addRoot(45);
       Position<Integer> p2 = T.addLeft(p1, 35);
       Position<Integer> p3 = T.addLeft(p2, 25);    
       Position<Integer> p4 = T.addRight(p1, 55);
       Position<Integer> p5 = T.addRight(p2, 40);    
       Position<Integer> p6 = T.addLeft(p4, 50);
       Position<Integer> p7 = T.addLeft(p5, 38);
       Position<Integer> p8 = T.addRight(p7, 39);
       Position<Integer> p9 = T.addRight(p5, 42);
       Position<Integer> p10 = T.addRight(p9, 44);
       Position<Integer> p11 = T.addRight(p4, 60);
       Position<Integer> p12 = T.addRight(p11, 63);
            
       // display the preorder traversal of T
       System.out.print("Preorder traversal of T: ");
       Iterable<Position<Integer>> itr2 = T.preorder();    
       for(Position<Integer> p : itr2)
           System.out.print(p.getElement() + " ");
       System.out.println();

// display the position visited after each element in a preorder traversal of T
       for(Position<Integer> p : itr2)
       {
           Position<Integer> result = T.preorderNext(p);
           if(result == null)
               System.out.println("preorderNext(" + p.getElement() + "): null");
           else
               System.out.println("preorderNext(" + p.getElement() + "): " + result.getElement());
       }
       System.out.println();
    
       // display the inorder traversal of T
       System.out.print("Inorder traversal of T: ");
       Iterable<Position<Integer>> itr1 = T.inorder();    
       for(Position<Integer> p : itr1)
           System.out.print(p.getElement() + " ");
       System.out.println();

OUTPUT:

( PLEASE VOTE FOR THIS ANSWER )

I THINK IT WILL BE USEFULL TO YOU ........

PLZZZZZ COMMENT IF YOU HAVE ANY PROBLEM I WILL TRY TO SOLVE IT ..........

THANK YOU ..........


Related Solutions

Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of...
Binary Tree Develop algorithms for performing various operations of binary tree like insertion and deletion of elements, finding an element in the binary tree. Analyse time and space complexity of the designed algorithm Write C++ program to implement binary tree
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Draw a (single) binary tree T, such that  Each internal node of T stores a...
Draw a (single) binary tree T, such that  Each internal node of T stores a single character  A preorder traversal of T yields ALGORITHMS  An inorder traversal of T yields GOLATIHRMS
Write a solution to return the count of the number of nodes in a binary tree....
Write a solution to return the count of the number of nodes in a binary tree. Your method will be passed one parameter, a copy of a pointer to the root node of the tree (Node *) and will return an int that is the count of nodes. If the tree is empty, return 0 (this is the recursive base case).
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels...
(Test perfect binary tree) A perfect binary tree is a complete binary tree with all levels fully filled. Define a new class named BSTWithTestPerfect that extends BST with the following methods: (Hint: The number of nodes in a perfect binary tree is 2^(height+1) - 1.) /** Returns true if the tree is a perfect binary tree */ public boolean isPerfectBST() Class Name: Exercise25_03
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) +...
Introduction to Algorithms - Analysis of Algorithms Solve the following recurrence relation: T(n) = T(an) + T((1 - a)n) + n
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
C++ Build a binary tree using a binary tree class member function from the following array...
C++ Build a binary tree using a binary tree class member function from the following array of preorder traversal 3,9,20,15,7 and inorder traversal 9,3,15,20,7. Implement the constructor, destructor and all member functions including print postorder traversal of the binary tree.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT