Data Structures Trees Question:

Download Job Interview Questions and Answers PDF

Explain Trees using C++ with an example?

Data Structures Trees Interview Question
Data Structures Trees Interview Question

Answer:

Tree is a structure that is similar to linked list. A tree will have two nodes that point to the left part of the tree and the right part of the tree. These two nodes must be of the similar type.

The following code snippet describes the declaration of trees. The advantage of trees is that the data is placed in nodes in sorted order.

struct TreeNode
{
int item; // The data in this node.
TreeNode *left; // Pointer to the left subtree.
TreeNode *right; // Pointer to the right subtree.
}

The following code snippet illustrates the display of tree data.

void showTree( TreeNode *root )
{
if ( root != NULL ) { // (Otherwise, there's nothing to print.)
showTree(root->left); // Print items in left sub tree.
cout << root->item << " "; // Print the root item.
showTree(root->right ); // Print items in right sub tree.
}
} // end inorderPrint()

Download Data Structures Trees Interview Questions And Answers PDF

Previous QuestionNext Question
Can you explain implementation of deletion from a binary tree?What is a B tree?