Homechevron_rightEngineeringchevron_rightComputer Sciencechevron_rightData Structurechevron_rightIn binary search tree which traversal is used for getting ascending or...

In binary search tree which traversal is used for getting ascending or...

  • Q. In binary search tree which traversal is used for getting ascending order values?
  • filter_dramaExplanation
    Answer is : A

    Concept:

    By the definition of a Binary Search Tree (BST), the value of left child node <= root node < right child node or left child node < root node <= right child node.

    In inorder traversal, we visit left node first, then root node and then right node. Hence, the inorder traversal always returns values in ascending order if it is run on a Binary Search Tree.

    Explanation:

    Binary Search Tree:

    Apply Inorder Traversal

    This will print 1-2-3.

    This will print 1-2-3-4 and then 1-2-3-4-5-6-7

    Output of Traversal: 1-2-3-4-5-6-7 (sorted order)

    Important Points:

    There are usually three methods of traversing a BST - Preorder, Postorder, Inorder.

    • In preorder, we visit nodes in the order of Root -> Left -> Right
    • In postorder, we visit nodes in the order of Left -> Right -> Root
    • In inorder, we visit nodes in the order of Left -> Root -> Right

Discussion

    No one started the discussion yet. Break the ice and start the conversation.
    Please Login to be part of the discussion.

Similar Questions

  • 1. With respect to linked lists and arrays, which of the following statements is INCORRECT?
  • filter_dramaExplanation
    Answer is : A

    Answer Option 1

    Concept

    Array

    Linked Lists ( Singly linked list )

    1. Array supports Random access to its elements.

    1. Linked list does not support Random access to its elements

    2. Array elements are stored in a contiguous manner.

    2.  Linked list elements are generally not stored in contiguous locations.

    3. In Arrays, new elements cannot be added dynamically due to their fixed size.

    3. In Linked Lists, an element can be easily added dynamically as the Linked list can dynamically grow or shrink as when needed

    4. Inserting a new element in an array is expensive compared to inserting a new element in a linked list. Because size is fixed we have to create a new Array entirely and copy all the elements to a new array along with the new element

    4. Inserting a new element in a Linked List is very simple. Just allocate space for the new node and update the references/pointers.

    5. Deleting an element from an array is expensive compared to deleting an element from a linked list. Because suppose we want to delete the very first element from the array then we need to shift all the right elements to one position left.

    5. Deletion in the linked list is simple just deallocate the node and change the references/pointers.

    6. In Arrays there is no extra space/variables is allocated.

    6. In the Linked List, pointers are maintained for every node in order to keep track of all the nodes in the list.

     

    Explanation

    Option 1: The linked list is slower in add and remove, but faster in get.

    This is the only option that is incorrect since Linked list adding and removing the given element is faster as compared to an array but finding a particular indexed element we might have to traverse to entire list but in case of array random access possible.

  • 2. Which is compulsory function in C language? 
  • filter_dramaExplanation
    Answer is : A

    The correct answer is main().

     Key Points

    • When the operating system runs a program in C, it passes control of the computer over to that program.
    • It is not possible to execute the c program without main().

     Additional Information

    • In C programming, scanf() is one of the commonly used function to take input from the user.
    • The scanf() function reads formatted input from the standard input such as keyboards.
    • The printf() is a library function to send formatted output to the screen.
    • The function prints the string inside quotations.
  • 3.

    A binary tree T has n leaf nodes, the number of nodes of degree 2 in T is:

  • filter_dramaExplanation
    Answer is : B

    In an "m-ary" tree. the number of total nodes (N) is given by

    N=mi + 1  ----(1)

    Where,

    i: Number of internal nodes

    Also, in a tree, N=i + L   ----(2)

    Where,

    L=number of leaf nodes

    Here m=2

    From equation (1) and equation (2);

    N = 2i + 1

    2i + 1 = i + L

    L = i + 1

    The number of leaves are 1 plus the number of internal nodes in binary tree.

    Here, given L=n, substitute above and we will get,

    i = L - 1

  • 4. Which of the following is the correct declaration of linked list?
  • filter_dramaExplanation
    Answer is : B

    Declaration syntax: For structure in C and C++

    struct tag_name

    {

    Datatype  variable_name;

    struct tag_name *pointer_variable;

    };

    Option 2:

    struct node

    {

    int data;

    struct node*link;

    }

  • 5. In binary search tree which traversal is used for getting ascending order values?
  • filter_dramaExplanation
    Answer is : A

    Concept:

    By the definition of a Binary Search Tree (BST), the value of left child node <= root node < right child node or left child node < root node <= right child node.

    In inorder traversal, we visit left node first, then root node and then right node. Hence, the inorder traversal always returns values in ascending order if it is run on a Binary Search Tree.

    Explanation:

    Binary Search Tree:

    Apply Inorder Traversal

    This will print 1-2-3.

    This will print 1-2-3-4 and then 1-2-3-4-5-6-7

    Output of Traversal: 1-2-3-4-5-6-7 (sorted order)

    Important Points:

    There are usually three methods of traversing a BST - Preorder, Postorder, Inorder.

    • In preorder, we visit nodes in the order of Root -> Left -> Right
    • In postorder, we visit nodes in the order of Left -> Right -> Root
    • In inorder, we visit nodes in the order of Left -> Root -> Right

Data StructureTopics

leaderboardLeaderboard
  • Rahul Kumar

    191 Points

  • VIKRAM JEET

    54 Points

  • GEETHIKA CHOWDARY

    53 Points

  • sunita saini

    52 Points

  • Zain

    49 Points