Create a Binary Tree Manually
📖 Scenario: You are building a simple binary tree to represent a family tree. Each node will hold a person's name as a string. You will create the tree manually by linking nodes together.
🎯 Goal: Build a binary tree manually by creating nodes and linking them as left and right children. Then print the tree nodes in preorder traversal.
📋 What You'll Learn
Create a struct called
Node with a string data and two Node* pointers called left and rightCreate three nodes with data
"Grandparent", "Parent", and "Child"Link the nodes so that
Grandparent is the root, Parent is the left child of Grandparent, and Child is the right child of ParentWrite a function
void preorder(Node* root) that prints the nodes in preorder (root, left, right)Call
preorder with the root node and print the names separated by spaces💡 Why This Matters
🌍 Real World
Binary trees are used in many areas like family trees, decision trees, and organizing data hierarchically.
💼 Career
Understanding how to create and traverse trees is fundamental for software development, especially in areas like databases, compilers, and AI.
Progress0 / 4 steps