
Trees 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.
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
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.