You are on page 1of 9

Republic of the Philippines

NUEVA VIZCAYA STATE UNIVERSITY


Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021

COLLEGE OF ARTS AND SCIENCES


Bayombong Campus

DEGREE BSIT COURSE NO. IT ECC 4


PROGRAM
SPECIALIZATION Web Dev and Network COURSE Data Structure and Algorithm
Design and Management TITLE
YEAR LEVEL 1st Year TIME FRAME 5 WK 16-17 IM 08
hours NO. NO.

I. CHAPTER VIII- B-TREE

II. LESSON TITLE


A. Anatomy of B-Tree
B. B-Tree operation
C. Implementation of B-Tree

III. LESSON OVERVIEW


This lesson will give the students a short knowledge on how to develop a program using the queue data
structure, to be familiar with the different operations and to illustrate the flow on how elements store and
retrieve in the data structure.

IV. DESIRED LEARNING OUTCOMES

At the end of the lesson, the student should be able to:

1. To simulate actual structure of elements in the queue


2. To apply the basic operation of B-Tree Data Structure

V. LESSON CONTENT

CHAPTER VIII: B-TREE DATA STRUCTURE using JAVA

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.

Node Properties of a Tree


o Root
 node without parent
“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 1 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021
 maximum depth of any node
o Internal node
 node with at least one child
o External node (also called as leaf node):
 node without children
 has no children -- its only path is up to its parent
o Ancestors of a node
 parent, grandparent, grand-grandparent, etc
o Descendant of a node
 child, grandchild, grand-grandchild, etc
o depth of a node
 is the length of the path (or the number of edges) from the root to that node
 number of ancestors
o height of a node
 is the longest path from that node to its leaves
 the height of a tree is the height of the root

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.

Parts of a Binary Tree

1. “Node” is the term used to describe a termination point.

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

Figure 9-2: A binary tree is comprised of several nodes, each of which


are related to other nodes on the tree.
Programmers determine the parent-child relationship by selecting a node, which is called
the current node. The node that spawns the current node is called the current node’s parent
node while the node or nodes spawned by the current node is called the child node.

2. Depth and Size

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.

Figure 9.3. The number of levels in a tree defines a tree’s depth,


and the number of nodes defines the size of the tree

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.

 Binary Search Tree

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:

For example, to find the node 40:

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:

For example, to add the node 25:


1. the root node is 50, which is greater than 25, so you go to 50's left child
2. 50's left child is 30, which is greater than 25, so you go to 30's left child
3. 30's left child is 20, which is less than 25, so you go to 20's right child
4. 20's right child doesn't exist, so you add 25 there

25
 Deletion

It is assumed that you have already found the node that you want to delete, using the search
technique described above.

Case 1: The node you want to delete is a leaf

“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

For example, do delete 40:

1. Simply delete the node!

Case 2: The node you want to delete has one child

Directly connect the child of the node that you want to delete, to the parent of the node that you
want to delete.

For example, to delete 90...

1. Delete 90, then make 100 the child node of 50.

Case 3: The node you want to delete has two children

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.

For example, to delete 30:

1. The right node of the node which is being deleted is 40.


2. (From now on, we continually go to the left node until there isn't another one...) The first left
node of 40, is 35.
3. 35 has no left node, therefore 35 is the successor!
“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 6 of 9
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2020-2021

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.

For example, to delete 30

1. Move 40 up to where 30 was.


2. 20 now becomes 40'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:

pre-order: 50, 30, 20, 40, 90, 100


in-order: 20, 30, 40, 50, 90, 100
post-order: 20, 40, 30, 100, 90, 50
level order: 50, 30, 90, 20, 40, 100

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

You might also like