Professional Documents
Culture Documents
V. LESSON CONTENT
If you are going to our school, you travel down the road a bit before seeing a fork in the road. You then
have two choices: bear left and you’ll be on campus; bear right and you’ll be lost. What does this have to do
with a tree data structure? A tree data structure is similar to the road because it provides you with a series of
forks in the road that lead you down a path to reach a decision.
A. WHAT IS A TREE?
A tree is a non-empty set, one element of which is designated the root of the tree while the remaining
elements are partitioned into non-empty sets each of which is a subtree of the root. In computer
science, a tree is an abstract model of a hierarchical structure which consists of nodes with a
parent-child relation.
Applications of a Tree
o Organization charts
o File systems
o Programming environments
B. TYPES OF TREES
Binary Tree
Each node has zero, one, or two children. This assertion makes many tree operations simple and
efficient.
It is a tree where each stem has not more than two branches. Typically, the stem has two branches, but
there can be situations when the stem has one branch or simply terminates, resulting in no additional
branches.
Figure 9.1. A binary tree is a tree where each stem has no more than two branches.
There are three kinds of termination points in a binary tree: the starting node, the ending
node, and the branch node. The starting node is called the root node, which is the top-level node in
the tree. The stem leading from the root node leads to the branch node.
The branch node is the fork in the road that links the root node to two branches. Each
branch terminates with a child node. Programmers call these the left branch and the right branch.
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 2 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
A binary tree is described by using two measurements: tree depth and tree size. The
tree depth is the number of levels in the tree. A new level is created each time a current node
branches to a child node. For example, one level is created when the root node branches into
child nodes.
The tree size is the number of nodes in the tree. Programmers estimate the size of a
tree by using the following formula
size » 2 depth
The size is an approximation because the tree may or may not be balanced. A balanced
tree is a binary tree where each current node has two child nodes. An unbalanced
tree is a binary tree where one or more current nodes have fewer than two child
nodes.
A binary tree where any left child node has a value less than its parent node and any right child
node has a value greater than or equal to that of its parent node
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 3 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
A typical binary search tree looks like this:
Terms
Node Any item that is stored in the tree.
Root The top item in the tree.
Child Node(s) under the current node.
Parent The node directly above the current node.
Leaf A node which has no children.
C. OPERATIONS IN TREES
Searching
Steps:
1. Start at the root node
2. If the item that you are searching for is less than the root node, move to the left child of the
root node, if the item that you are searching for is more than the root node, move to the right
child of the root node and if it is equal to the root node, then you have found the item that
you are looking for.
3. Now check to see if the item that you are searching for is equal to, less than or more than
the new node that you are on. Again if the item that you are searching for is less than the
current node, move to the left child, and if the item that you are searching for is greater than
the current node, move to the right child.
4. Repeat this process until you find the item that you are looking for or until the node doesn't
have a child on the correct branch, in which case the tree doesn't contain the item which you
are looking for.
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 4 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
Example:
1. the root node is 50, which is greater than 40, so you go to 50's left child
2. 50's left child is 30, which is less than 40, so you next go to 30's right child
3. 30's right child is 40, so you have found the item that you are looking for
Insertion or Adding
Steps:
1. To add an item, you first must search through the tree to find the position that you should
put it in. You do this following the steps above.
2. When you reach a node which doesn't contain a child on the correct branch, add the new
node there.
Example:
25
Deletion
It is assumed that you have already found the node that you want to delete, using the search
technique described above.
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 5 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
Directly connect the child of the node that you want to delete, to the parent of the node that you
want to delete.
Steps:
1. Find the left-most node in the right subtree of the node being deleted.
2. (After you have found the node you want to delete, go to its right node, then for every node
under that, go to its left node until the node has no left node)
3. From now on, this node will be known as the successor.
Case 4: The successor is the right child of the node being deleted
Steps:
1. Directly move the child to the right of the node being deleted into the position of the node being
deleted.
2. As the new node has no left children, you can connect the deleted node's left subtree's root as
it's left child.
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 7 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
Case 5: The successor isn't the right child of the node being deleted
Example:
To delete 30
1. Move the successor into the place where the deleted node was and make it inherit both of its
children. So 35 moves to where 30 was and 20 and 40 become its children.
2. Move the successor's (35) right subtree to where the successor was. So 37 becomes a child of
40.
Traversal
pre-order: Current node, left subtree, right subtree(CLR)
in-order: Left subtree, current node, right subtree.(LCR)
post-order: Left subtree, right subtree, current node(LRC)
level order: Level by level, from left to right, starting from the root node.
Examples:
XII. REFERENCES
Books:
1. Malik, D. S. (2013). C programming: program design including data structures. Boston, Mass:
Course Technology, Cengage Learning.
2. Dale, N., Joyce, D. T., & Weems, C. (2012). Object-Oriented Data Structures Using Java (3rd
Ed.). Jones & Bartlet Learning.Canada.
3. Drozdek, Adam, Data Structure and Algorithm in java, 2nd ed, Brooks/Cole Pub
4. Sahni, Sartaj, Data Structure, algorithm, and application in java, McGraw Hill
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 8 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
5. Preiss, Bruno, Data Structure and algorithms with object-oriented design pattern in java, Wiley
6. Data Structures Demystified, James Keogh & Ken Davidson, Copyright © 2004 by The
McGraw-Hill Companies
On-Line Resources:
1. cs-www.cs.yale.edu/homes/aspnes/classes/223/notes.pdf.
2. http://www.cs.tut.fi/~uds/material/uds_slides_4.pdf
3. http://en.wikipedia.org/wiki/Data_structure
4. http://freevideolectures.com/Course/2279/Data-Structures-And-Algorithms
5. http://courses.cs.vt.edu/csonline/DataStructures/Lessons/Introduction/index.html
6. http://www.tutorialspoint.com/java/java_data_structures.htm
7. http://www.cs.sunysb.edu/~skiena/214/lectures/
8. http://www.cs.utexas.edu/~novak/cs315contents.html
9. http://www.csd.uwo.ca/courses/CS2210b/
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 9 of 9