Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Returning node

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Returning node

    Working on a program "to convert a sorted array to binary search tree".
    How to get the value return from the node? It does not print what I want.


    Here is my code :

    public class ConvertToBinarySearchTree {
     
        public static class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
     
            TreeNode(int x) {
                this.val = x;
            }
        }
     
        public static TreeNode sortedArraytoBST(int[] nums) {
            if (nums.length == 0)
                return null;
            return constructTreeNodeFromArray(nums, 0, nums.length - 1);
        }
     
        public static TreeNode constructTreeNodeFromArray(int[] nums, int left, int right) {
            // checking the boundary
            if (left > right)
                return null;
            int midpoint = left + (right - left) / 2; // get the index of the root
            TreeNode node = new TreeNode(nums[midpoint]);
            node.left = constructTreeNodeFromArray(nums, left, midpoint - 1);
            node.right = constructTreeNodeFromArray(nums, midpoint + 1, right);
     
     
            return node; 
        }
     
        public static void main(String[] args) {
            int[] arr = { 1, 2, 3, 4, 5, 6 };
             System.out.println(sortedArraytoBST(arr));
        }
    }

    My output :

    ConvertToBinarySearchTree$TreeNode@6504e3b2
    Last edited by siid14; July 5th, 2022 at 06:27 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Returning node

    print what I want.
    Please explain what you want.

    The printed output looks like what is returned by the Object class's toString method. If you want something different, override the class's toString method and have it return the String you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Returning node

    Yes right, I just added the following method before your comment and it works :

      public static void traverseTree(TreeNode root) {
            if (root != null) {
                traverseTree(root.left);
                traverseTree(root.right);
                System.out.println(root.val);
            }
        }


    --- Update ---

    And I wanted each node printed as a Binary Search Tree. I got the output I wanted here :

    2
    1
    4
    6
    5
    3

  4. #4
    Member
    Join Date
    Apr 2022
    Posts
    36
    Thanks
    0
    Thanked 8 Times in 8 Posts

    Default Re: Returning node

    return node.val;

Similar Threads

  1. htmlProcessor node xml
    By -_-" in forum Other Programming Languages
    Replies: 2
    Last Post: June 25th, 2020, 09:34 AM
  2. Replacing Node Value
    By ProgrammablePeter in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 30th, 2014, 05:50 PM
  3. [SOLVED] XML get/set node by attribute value
    By b_jones10634 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 24th, 2010, 11:26 AM
  4. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM
  5. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM

Tags for this Thread