Explain Trees using C++ with an example?
Submitted by: AdministratorTree 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()
Submitted by: Administrator
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()
Submitted by: Administrator
Read Online Data Structures Trees Job Interview Questions And Answers
Top Data Structures Trees Questions
☺ | Explain red-black trees? |
☺ | How to find the depth of a binary tree? |
☺ | What is threaded binary tree. Explain its common uses? |
☺ | Do you know implementation of traversal of a binary tree? |
☺ | Explain B+ tree? Explain its uses? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |