Menu

Question Discussion & Solution

MCQ
Q.
Select the code snippet that performs pre-order traversal iteratively.

a)

public void preOrder(Tree root) 
{   
        if (root == null) return;
 Stack stk = new Stack();
        st.add(root);        
 while (!stk.empty()) 
        {
            Tree node = stk.pop();           
            System.out.print(node.data + " ");
   if (node.left != null) stk.push(node.left);
            if (node.right != null) stk.push(node.right);
        }
}

b)

public void preOrder(Tree root) 
{   
        if (root == null) return;
  Stack stk = new Stack();      
 while (!stk.empty()) 
        {
            Tree node = stk.pop();           
            System.out.print(node.data + " ");
            if (node.right != null) stk.push(node.right);
            if (node.left != null) stk.push(node.left);
        }
}

c)

public void preOrder(Tree root) 
{   
        if (root == null) return;
 Stack stk = new Stack();
        st.add(root);        
 while (!stk.empty()) 
        {
            Tree node = stk.pop();           
            System.out.print(node.data + " ");
            if (node.right != null) stk.push(node.right);
            if (node.left != null) stk.push(node.left);
        }
}

d) None of the mentioned

forum Community Discussion

speaker_notes_off

No discussions yet. Be the first to start!

You must be logged in to participate in the discussion.

login Login to Discuss

auto_awesome Similar Questions

MCQ
1.
A list integers is read in, one at a time, and a binary search tree is constructed. Next the tree is traversed would result in a printout which duplicates the original order of the list of integers?
forum Discussion
MCQ
2.
What are splay trees?
forum Discussion
MCQ
3.
When is it appropriate to use direct addressing?
forum Discussion
MCQ
4.
Elements in a tree can be indexed by its position under the ordering of the keys and the ordinal position of an element can be determined, both with good efficiency.
forum Discussion
MCQ
5.
The following formula is of :

left_subtree (keys) ? node (key) ? right_subtree (keys)
forum Discussion

category More Data Structure Topics

article

Data Structure Basics

format_list_bulleted 128 MCQs
article

Abstract data types

format_list_bulleted 37 MCQs
article

Arrays

format_list_bulleted 134 MCQs
article

Lists

format_list_bulleted 166 MCQs
article

Stacks

format_list_bulleted 165 MCQs
article

Queues

format_list_bulleted 61 MCQs
article

Trees

format_list_bulleted 133 MCQs
article

Binary Trees

format_list_bulleted 120 MCQs
article

B Trees

format_list_bulleted 8 MCQs
article

Heaps

format_list_bulleted 36 MCQs
article

Hash based structures

format_list_bulleted 7 MCQs
article

Graphs

format_list_bulleted 157 MCQs
article

Mixed

format_list_bulleted 10 MCQs