Trees Programming Practice Problems
"Herbs, shrubs, bushes, and trees."
— Dont imagine lush greenery 😁!
Beginner Problems
- Write a program that finds the height of a binary tree.
- Write a program that counts the number of nodes in a binary tree and the number of leaf nodes in a binary tree.
- Given a binary tree, create another binary tree that is a mirror image of the given tree.
- Write a program that implements the non-recursive form of the functions inorder(), preorder(), and postorder().
- Write a program that maintains a dictionary of words as a binary tree.
- Given any number, find whether that number is present in the binary tree. If present then find the level at which it is present.
- Given two binary trees, write a program that finds whether the two binary trees are similar.
- Write a program that finds the mirror images of each other.
- Write a program that finds the number of nodes in a binary tree at each level.
- Write a program that traverses a binary tree level by level, from left to right.
- Write a function to insert a node t as a left child of any node s in a threaded binary tree.
- Construct a BST: Construct a BST by inserting the following sequence of numbers:
36, 26, 46, 21, 31, 41, 56, 11, 24, 51, 66
- Using the BST you just created:
- Show the steps to search for the number 41.
- Delete the node with the value 21 (a node with two children).
- Delete the node with the value 56 (a node with one child).
- Delete the node with the value 11 (a leaf node).
- How do you find the minimum and maximum element in a BST? Write the algorithms.
- Write a recursive C function to search for a given key in a BST.
- Write an iterative C function to insert a new node into a BST.
Binary Search Tree
- Implement a recursive function to create a BST and insert an item, ensuring duplicates are not allowed.
- Implement both a recursive and an iterative function to search for a key in a BST, returning a pointer to the node or NULL if not found.
- Write a function to find the maximum element in a BST (the rightmost node).
- Write a function to find the minimum element in a BST (the leftmost node).
- Implement an iterative function, Insert, to insert a new node into a BST after checking for duplicates and finding the correct position.
- Implement a function, Delete, to delete a node from a BST, considering the three cases: deleting a leaf node, a node with one child, and a node with two children (e.g., replacing it with the inorder successor, the smallest element in the right subtree).
General Binary Tree Operations and Traversals
- Implement a recursive function, CreateBinaryTree, to create a general binary tree based on user input for child nodes (e.g., using -1 as a sentinel value).
- Implement a function, insert, to insert a new node at a position specified by a string of directions (e.g., "LLR") from the root.
- Implement a recursive function, inorder, and an iterative function, iterative_inorder, for inorder traversal (LVR).
- Implement a recursive function, preorder, and an iterative function for preorder traversal (VLR).
- Implement a recursive function, postorder, and an iterative function for postorder traversal (LRV).
- Implement a function, Levelorder, for level-order traversal using a queue.
- Implement a recursive function, height, to find the height (or depth) of a binary tree.
- Implement a recursive function, copy, to create an exact copy of a given binary tree.
- Implement a function, Equal, to test if two binary trees are equal (same topology and identical node data).
- Implement a function, count_nodes, to count the total number of nodes in a tree by traversing it.
- Implement a function, count_leafnodes, to count the number of leaf nodes in a binary tree.
Expression Trees
- What is an Expression Tree? What do the leaf nodes and internal nodes represent?
- Construct an expression tree for the infix expression:
((A + B) * (C - D))
- Given the postfix expression
construct the corresponding expression tree.
A B + C D E + * *
- If you perform an inorder traversal on an expression tree, what kind of expression do you get? What about postorder and preorder?
Advanced Topics
- What is the primary motivation for using a Threaded Binary Tree? What problem does it solve?
- Explain the difference between a right-threaded (one-way) and a two-way threaded binary tree.
- Given the binary tree on page 96, draw its corresponding right-threaded binary tree, clearly indicating the threads with dotted lines.
- What is the purpose of the boolean flag(s) (e.g., rightThread) in the node structure of a threaded binary tree?
- Explain the Array Representation of a binary tree. If a node is at index i, what are the indices of its left and right children?
- Describe the Left Child - Right Sibling representation for general trees.