0% found this document useful (0 votes)
30 views

Assignments 4 Tree

The document discusses different tree data structures and operations on trees. It provides examples and instructions on how to: 1. Write a program to perform operations like calculating height, preorder traversal, postorder traversal, and check if a tree is binary using a tree with given nodes and edges as input. 2. Draw and modify a binary search tree by inserting and deleting nodes using a given list of numbers as an example. 3. Draw and modify a heap tree by inserting and deleting nodes using the same list of numbers. 4. Insert random numbers into an empty binary search tree one by one and draw each step, then explain how to find and remove the maximum and minimum elements. 5.

Uploaded by

thanhvucute2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignments 4 Tree

The document discusses different tree data structures and operations on trees. It provides examples and instructions on how to: 1. Write a program to perform operations like calculating height, preorder traversal, postorder traversal, and check if a tree is binary using a tree with given nodes and edges as input. 2. Draw and modify a binary search tree by inserting and deleting nodes using a given list of numbers as an example. 3. Draw and modify a heap tree by inserting and deleting nodes using the same list of numbers. 4. Insert random numbers into an empty binary search tree one by one and draw each step, then explain how to find and remove the maximum and minimum elements. 5.

Uploaded by

thanhvucute2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Homework lecture 4

Trees
1. Given a tree, your task is to write a program with following functions
● Calculate the height of the tree.

● Write out the order of nodes following the preorder traversal.

● Write out the order of nodes following the postorder traversal.

● Check if the given tree is a binary tree? If yes, write out the order of nodes in inorder
traversal.
Input: Data come from the keyboard:
- The first line contains integer numbers N, M indicating the number of nodes, and edges,
respectively.
- M following lines each contains two integer numbers u, v indicating that u is the parent of
v.
Output: Data are written to the screen as following:
- The height of the tree
- The order of nodes in preorder traversal
- The order of nodes in postorder traversal
- The order of nodes in the inorder if the tree is binary. Otherwise, write the string ‘NOT
BINARY TREE’

Keyboard Screen
54 2
12 12453
13 45231
24 4 2513
25

2. Given a list of integer numbers: 2, 1, 10, 6, 3, 8, 7, 13, 20.


- Draw the binary search tree
- Draw the binary search tree after inserting values: 14, 0, 35
- Draw the binary search tree after deleting: 6, 13, 35

3. Given a list of integer numbers: 2, 1, 10, 6, 3, 8, 7, 13, 20.


- Draw the heap tree
- Draw the heap tree after inserting values: 14, 0, 35
- Draw the heap tree after deleting: 6, 13, 35

4. Use random.org to generate a set of 10 integers from 1-20 (S1).


- Insert elements from S1 to a binary search tree one by one and draw the binary search
tree after each step.
- Write out the procedure to find and remove the maximum element from binary search
tree in detail.
- Write out the procedure to find and remove the minimum element from binary search tree
in detail.

5. Use random.org to generate another set of 10 integers from 1-20 (S2).


- Draw the heap (tree) from S2
- Insert elements from S1 to this heap one by one and draw the heap after each step.
- Write out the procedure to find and remove the maximum element from binary search
tree in detail.

You might also like