0% found this document useful (0 votes)
18 views5 pages

Lab VII - COSC 215

The document outlines a lab session for COSC 215 focused on Binary Trees and Binary Search Trees. It includes exercises to implement methods for checking tree properties, constructing trees with specified values, and performing various tree operations. Sample outputs and actions for both binary trees and binary search trees are provided to guide students in their programming tasks.

Uploaded by

mmezher50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Lab VII - COSC 215

The document outlines a lab session for COSC 215 focused on Binary Trees and Binary Search Trees. It includes exercises to implement methods for checking tree properties, constructing trees with specified values, and performing various tree operations. Sample outputs and actions for both binary trees and binary search trees are provided to guide students in their programming tasks.

Uploaded by

mmezher50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Affordable Excellence in Education

COSC 215 – Advanced Programming and Data Structures

Lab Session VII


Binary Tree and Binary Search Tree

Dr. Ali Rammal (Assistant Professor – CIS Department)


Exercise 1: Binary Tree
1. Consider the class BinaryTree given in the lectures (posted on Moodle). Add to it the
following methods:
a. Method isBalanced that checks whether the tree is balanced. A balanced binary
tree is defined as a binary tree in which the height of the left and right subtree of
any node differ by not more than 1

b. Method isPerfect that checks whether the tree is perfect. A perfect binary tree is
a special type of binary tree in which all the leaf nodes are at the same depth, and all
non-leaf nodes have two children. In simple terms, this means that all leaf nodes are
at the maximum depth of the tree, and the tree is completely filled with no gaps. A
perfect tree of height h has total nodes = 2h – 1

2. Develop a main function to perform the following actions:


a. Construct the tree T1 containing the following integer values:

2
b. Construct the tree T2 containing the following characters values:

c. Construct the tree T3 containing the following characters values:

d. Display each tree in preorder, inorder, and postorder traversals


e. Display the number of nodes of each tree
f. Display the number of leaves of each tree
g. Display the height of each tree
h. Display whether each tree is balanced
i. Display whether each tree is perfect

Sample output

Constructing T1
...
Preorder T1...
1 2 4 5 3 6 7
Inorder T1...
4 2 5 1 6 3 7
Postorder T1...
4 5 2 6 7 3 1
T1 size: 7
T1 Number of leaves: 4
T1 Height: 3
T1 is balanced? Yes
T1 is perfect? Yes

Constructing T2
...
Preorder T2...
A B F C D E
Inorder T2...

3
F B A D C E
Postorder T2...
F B D E C A
T2 size: 6
T2 Number of leaves: 3
T2 Height: 3
T2 is balanced? Yes
T2 is perfect? No

Constructing T3
...
Preorder T3...
A B F G C D E
Inorder T3...
G F B A D C E
Postorder T3...
G F B D E C A
T3 size: 7
T3 Number of leaves: 3
T3 Height: 4
T3 is balanced? No
T3 is perfect? No

Exercise 2: Binary Search Tree - BST


1. Consider the class BST given in the lectures (posted on Moodle). Add to it the following
methods:
a. The iterative version of the method search
b. The recursive version of the method minimum
c. A method that returns the count of nodes containing a value greater than a key
passed as parameter.
d. A method that takes as parameter an array of keys and produce the corresponding
BST by inserting the keys of the array one by one
2. Develop a main function to perform the following actions:
a. Create a BST of integers T1 containing the following keys {31, 62, 3, 88, 17, 23,
20, 51, 28, 8, 5, 44, 40}
b. Create a BST of strings T2 containing the following keys {"this", "is", "a", "bst",
"of", "strings", "it", "is", "cool", "and", "quick"}
c. Display both trees in preorder, inorder, and postorder traversals
d. Display the minimum key of each tree
e. Search for the keys 18 and 44 in T1
f. Search for the keys “warm” and “cool” in T2
g. Display the count of nodes, in both trees, containing a value greater than a key
entered by the user
h. Delete the roots of both trees
i. Display both trees in preorder traversal
4
Sample input/output

Producing T1
...
Preorder...
31 3 17 8 5 23 20 28 62 51 44 40 88
Inorder...
3 5 8 17 20 23 28 31 40 44 51 62 88
Postorder...
5 8 20 28 23 17 3 40 44 51 88 62 31
Minimum of T1 is 3
18 is found in T1? No
44 is found in T1? Yes
Enter an integer key: 19
The count of keys, of T1, > 19 is: 9
Removing root of T1...
Preorder...
40 3 17 8 5 23 20 28 62 51 44 88

Producing T2
...
Preorder...
this is a bst and cool of it strings quick
Inorder...
a and bst cool is it of quick strings this
Postorder...
and cool bst a it quick strings of is this
Minimum of T2 is a
"warm" is found in T2? No
"cool" is found in T2? Yes
Enter a string key: binary
The count of keys, of T2, > binary is: 8
Removing root of T2...
Preorder...
is a bst and cool of it strings quick

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy