Some exercises about creating and traversing trees in C.
This C program allows users to input values to dynamically create a binary tree. The program then performs in-order, pre-order, and post-order traversals on the constructed tree.
Header file containing function prototypes and the definition of the TreeNode struct for binary tree nodes.
-
createNode
:- Description: Creates a new tree node with the provided key as data.
- Parameters:
- key: A number to add as data in the tree node.
- Return: A pointer to the created TreeNode.
-
inorderTraversal
:- Description: Prints data using in-order traversal (Left-Root-Right).
- Parameters:
- root: Pointer to the root of the tree.
- Return: void.
-
preorderTraversal
:- Description: Prints data using pre-order traversal (Root-Left-Right).
- Parameters:
- root: Pointer to the root of the tree.
- Return: void.
-
postorderTraversal
:- Description: Prints data using post-order traversal (Left-Right-Root).
- Parameters:
- root: Pointer to the root of the tree.
- Return: void.
-
insertNode
:- Description: Inserts a key into the binary tree.
- Parameters:
- root: Pointer to the root of the tree.
- data: The value to insert into the tree.
- Return: A pointer to the modified tree.
-
getSize
:- Description: Gets the size of the tree from user input.
- Parameters:
- msg: A message to be printed for user input.
- Return: The size of the tree.
-
getValues
:- Description: Gets values from the user and inserts them into the tree.
- Parameters:
- root: Pointer to the root of the tree.
- size: The size of the tree.
-
printUsageTraversal
:- Description: Prints the usage traversal corresponding to the user's option.
- Parameters:
- root: Pointer to the root of the tree.
- option: The option of the user.
- Return: void.
-
searchNode
:- Description: Searches for a key in the binary tree and returns true if the key is found, and false otherwise.
- Parameters:
- root: Pointer to the root of the tree.
- key: The value to search for in the tree.
- Return: true if the key is found, false otherwise.
-
minNode
:- Description: Finds the minimum value in the binary tree.
- Parameters:
- root: Pointer to the root of the tree.
- Return: The minimum value found in the tree.
-
maxNode
:- Description: Finds the maximum value in the binary tree.
- Parameters:
- root: Pointer to the root of the tree.
- Return: The maximum value found in the tree.
-
nbrNode
:- Description: Counts the number of nodes in the binary tree.
- Parameters:
- root: Pointer to the root of the tree.
- Return: The total number of nodes in the tree.
-
heightTree
:- Description: Returns the height of the tree.
- Parameters:
- root: Pointer to the root of the tree.
- Return: The height of the tree.
-
createNodeList
:- Description: Creates a new node for a linked list with the provided value.
- Parameters:
- val: A value to add in the linked list node.
- Return: A pointer to the created Node.
-
insertNodeList
:- Description: Inserts a value into a linked list.
- Parameters:
- head: Pointer to the head of the linked list.
- val: The value to insert into the linked list.
- Return: A pointer to the modified linked list.
-
printList
:- Description: Prints the elements of a linked list.
- Parameters:
- head: Pointer to the head of the linked list.
- Return: void.
-
splitTreeToLists
:- Description: Classifies elements of a binary tree into two linked lists based on a certain condition.
- Parameters:
- root: Pointer to the root of the tree.
- list1: Pointer to the head of the first linked list.
- list2: Pointer to the head of the second linked list.
- val: The value used as a condition for classification.
- Return: void.
Source file containing the main function to run the program.
Refer to tree.h for function descriptions.
gcc -o tree *.c
-
Gets the number of nodes from the user:
- Run the program by executing the following commands in your terminal:
./tree
- Enter the number of nodes when prompted and press
Enter
.
- Run the program by executing the following commands in your terminal:
-
Gets values from the user and inserts them into the tree:
- After entering the number of nodes, the program will prompt you to enter values for each node.
- For each prompt, enter a numerical value and press
Enter
.
-
Prompts the user to choose a traversal option:
- After entering the values, the program will prompt you with options for tree traversal:
Press: 1) for Inorder Traversal 2) for Preorder Traversal 3) for Postorder Traversal
- Enter the corresponding number for the desired traversal option and press
Enter
.
- After entering the values, the program will prompt you with options for tree traversal:
-
Prints the corresponding traversal:
- The program will then print the result of the selected traversal option based on the constructed binary tree.
- The result will be displayed in the console.
-
Additional Functions:
- The program also provides additional functions for further analysis of the binary tree:
-
Search for a Node:
- To search for a specific value in the tree, you can use the
searchNode
function.
- To search for a specific value in the tree, you can use the
-
Find Minimum Value:
- To find the minimum value in the tree, you can use the
minNode
function.
- To find the minimum value in the tree, you can use the
-
Find Maximum Value:
- To find the maximum value in the tree, you can use the
maxNode
function.
- To find the maximum value in the tree, you can use the
-
Count Number of Nodes:
- To count the number of nodes in the tree, you can use the
nbrNode
function.
- To count the number of nodes in the tree, you can use the
-
Calculate Height of the Tree:
- To calculate the height of the tree, you can use the
heightTree
function.
- To calculate the height of the tree, you can use the
-
Create a Linked List Node:
- To create a new node for a linked list, you can use the
createNodeList
function.
- To create a new node for a linked list, you can use the
-
Insert into a Linked List:
- To insert a value into a linked list, you can use the
insertNodeList
function.
- To insert a value into a linked list, you can use the
-
Print Elements of a Linked List:
- To print the elements of a linked list, you can use the
printList
function.
- To print the elements of a linked list, you can use the
-
Split Tree Elements into Linked Lists:
- To classify elements of a binary tree into two linked lists based on a certain condition, you can use the
splitTreeToLists
function.
- To classify elements of a binary tree into two linked lists based on a certain condition, you can use the
-
- The program also provides additional functions for further analysis of the binary tree:
Contributions are welcome! Feel free to open issues or submit pull requests.