Thread: Sorted binary tree

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Sorted binary tree

    I saw an example of a binary tree and I am wondering if it's sorted or not.

    Sorted binary tree-tree_c-png

    I am dangled with the all nodes thing the link says.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is not a binary search tree. Consider the node with the key 3: every node in its left subtree should have a key less than 3, but 5 > 3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    So my worries were correct. Thanks.

    However, another question just got in my mind. With this structure of the tree, how would we place the values (1 till 7), so that the tree would be ordered?
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, this is an ideal arrangement for a binary search tree with those values:
    Code:
       4
     2   6
    1 3 5 7
    EDIT:
    If by "this structure of the tree" you mean the alternating right/left children, then I suppose:
    Code:
    1
      7
    2
      6
    3
      5
    4
    except that the binary search tree would have effectively been degraded to be a linked list with extra pointers.
    Last edited by laserlight; 08-29-2014 at 09:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Yes I agree. My question however is how the tree I posted in the picture would be sorted. I mean let's say that we erase the values and we place them as we want in the picture, in which way should we place the values so that the tree be ordered?

    If I am not clear, let me know please.

    EDIT:
    Your edit is the answer!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I don't know why leading spaces on the first line are ignored by code (unless the view is changed to plain text), but to get around that issue, I use a dummy first line:

    Code:
    .......
       4
     2   6
    1 3 5 7

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by rcgldr View Post
    I don't know why leading spaces on the first line are ignored by code (unless the view is changed to plain text), but to get around that issue, I use a dummy first line:

    Code:
    .......
       4
     2   6
    1 3 5 7
    You just have to make sure there's a character in the very first spot:

    Code:
    .  4
     2   6
    1 3 5 7

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary search on a sorted list
    By littlefermat245 in forum C Programming
    Replies: 13
    Last Post: 01-25-2010, 03:49 AM
  2. Inserting value to a sorted binary tree?
    By HappySmileMan in forum C Programming
    Replies: 3
    Last Post: 02-15-2009, 09:34 AM
  3. Replies: 2
    Last Post: 08-01-2007, 01:08 PM
  4. display tree data in binary tree format
    By xddxogm3 in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2003, 12:47 AM
  5. Building a Binary Search Tree off sorted data
    By BlackCitan in forum C Programming
    Replies: 0
    Last Post: 12-01-2003, 09:52 PM