Question
answer in java
Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Tr
0 0
Add a comment Improve this question Transcribed image text
Answer #1

> Printing BST in descending order can be done using reverse inorder traversal of BST.
Let us take an example to understand the solution.

  -

This is a Binary Search Tree and following will be the inorder and reverse inorder of above binary search tree

  • Inorder (Left Root Right) ---> 1 2 3
  • Reverse Inorder (Right Root Left) ----> 3 2 1

As you can observe that Reverse Inorder of BST gives us nodes in descending order.

Code:

 public void reverseSorted(Treenode root){          
     //Check if root is null or not, if null then return            
     if(root == null)                       
        return;                                 

     //recursively call Right node first            
     reverseSorted(root.right);              

     //Print data of the root node      
     System.out.print(root.val+" ");           

     //At last, recursively call the Left node   
     reverseSorted(root.left);       
}

-

Add a comment
Know the answer?
Add Answer to:
answer in java Given a Binary Search Tree containing integer values, write a method to print...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode...

    The code is in JAVA public class CheckBST {   //method to implement public static boolean isValidBST(TreeNode root) { } public static void main(String[] args) { TreeNode a = new TreeNode(1); TreeNode b = new TreeNode(2); TreeNode c = new TreeNode(3); a.left = b; a.right = c; System.out.println(isValidBST(a)); TreeNode d = new TreeNode(2); TreeNode e = new TreeNode(1); TreeNode f = new TreeNode(3); d.left = e; d.right = f; System.out.println(isValidBST(d)); } } TreeNode.java class TreeNode { int val; TreeNode left; TreeNode...

  • Public class TreeNode { TreeNode left, right; Int val; } Given a binary tree, print val...

    Public class TreeNode { TreeNode left, right; Int val; } Given a binary tree, print val in level order. Input: 1 2 3 4 5 6 7 Out: 1234567

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • Have to write the tree into a text file? JAVA CODE Binary search tree This is...

    Have to write the tree into a text file? JAVA CODE Binary search tree This is the tree public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left;...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • In Java: Given the following binary tree class, write a recursive method called size() which will...

    In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }

  • Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to...

    Consider a binary search tree of positive integers without duplicates. Implement (write out) a program to find the second greatest element in the tree. The Second function is a member function of class BinarySearchTree. The function returns zero if the tree is empty or has only one element, otherwise it returns the value of the second greatest element. Based on the classes provided below, write the implementation of Second in the solution box, including any helper functions (if any) that...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT