w3resource

C Programming Tree Exercises: Binary Trees, Traversals, and AVL Trees

C Program to implement Tree Structure [10 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

From Wikipedia,

In computer science, a tree is a widely used abstract data type that represents a hierarchical tree structure with a set of connected nodes. Each node in the tree can be connected to many children (depending on the type of tree), but must be connected to exactly one parent, except for the root node, which has no parent (i.e., the root node as the top-most node in the tree hierarchy). These constraints mean there are no cycles or "loops" (no node can be its own ancestor), and also that each child can be treated like the root node of its own subtree, making recursion a useful technique for tree traversal. In contrast to linear data structures, many trees cannot be represented by relationships between neighboring nodes (parent and children nodes of a node under consideration if they exists) in a single straight line (called edge or link between two adjacent nodes).

Visual Presentation:

 C Tree data structure.

1. Write a C program that creates a binary tree. Allow users to input nodes and build a binary tree structure.
Click me to see the solution

2. Write a C program to perform an in-order traversal of a binary tree. Print the elements in sorted order.
Click me to see the solution

3. Write a C program that extends the binary tree program to support the insertion of elements. This is in a way that maintains the binary search tree property.
Click me to see the solution

4. Write a C program to calculate the height of a binary tree. Ensure the program handles empty trees gracefully.
Click me to see the solution

5. Write a C program that implements a deletion function for a binary tree. Allow users to delete nodes while maintaining the binary search tree structure.
Click me to see the solution

6. Write a C program to create a mirror image of a binary tree. Print both the original and mirrored trees.
Click me to see the solution

7. Write a C program that extends the binary tree program to perform a level-order traversal. Print the nodes at each level from top to bottom.
Click me to see the solution

8. Write a C program to build an expression tree from a postfix expression. Evaluate and display the results.
Click me to see the solution

9. Write a C program to determine if a binary tree has a root-to-leaf path whose sum equals a given target sum.

Click me to see the solution

10. Write a C program that implements an AVL tree in C. Include functions for insertion and deletion while maintaining the AVL tree's balance property.

Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.