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
  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • Java class TreeNode is defined as below: public class TreeNode {     public int val;     public TreeNode...

    Java class TreeNode is defined as below: public class TreeNode {     public int val;     public TreeNode left, right;     public TreeNode(int x) { val = x; }     public TreeNode(int x, TreeNode lChild, TreeNode rChild) {         val = x;         left = lChild;         right = rChild;     } } Suppose you only have the reference of the root, please write a method that returns a deep copy of the binary tree. Your method returns the root of the deep copy. Your algorithm should...

  • Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree...

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Use following Node class, no height is stored in the Node /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; }...

  • 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)...

  • You are given the root of a Binary Search Tree. Print the leaf elements of the...

    You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public:         int name; Node* left = NULL;         Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...

  • Implement a method (in java) to print all the element of a binary search tree in...

    Implement a method (in java) to print all the element of a binary search tree in pre-order traversal order. (CANNOT TAKE A PARAMETER) Thank you! public void printTree() { //chegg code here only! }

  • 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) =...

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