LESSON 5 TREES
Specific Learning Outcomes:
• Apply the key concepts of trees
• Create and implement a binary search tree (BST)
• Insert and delete a node into the BST
• Traverse a tree data structure
TREES
Definition (Non recursive definition of a tree)
A directed graph (digraph D) is called a rooted tree (or tree) if D contains a node u, called the root, such that for every
node v of D, there is a unique u-v path in D. The number of edges that must be followed is the path length.
Definition
The root v0 is said to be at depth 0. No edges enter v0, but several may leave, and we draw these edges downward. The
terminal nodes of the edges beginning at v0 are called the depth 1 nodes. The terminal nodes of the edges beginning at
depth i nodes are called depth i+1 node.
Definition
The terminal nodes of the edges beginning at node v are called the children of v. In other words, v is the parent of these
terminal nodes. Nodes with the same parent are siblings.
Definition
The nodes of the tree that have no children are called the leaves of the tree. Otherwise, they are called the interior
nodes.
Definition
The height of a node is the length of path from the node to the deepest leaf. The height of a tree is equal to the height
of the root.
Definition
If there is a path from node u to node v, then u is an ancestor of v and v is a descendant of u. If u v, then u is a proper
ancestor of v and v is a proper descendant of u. The size of a node is equal to the number of descendants it has
(including the node itself).
Asian College of Technology
College of Computer Studies
Definition (Recursive definition of a tree)
A tree is a finite set of elements that is either:
- empty; or
- consists of a root and zero or more non-empty subtrees T1, T2, ...., Tk, each of whose roots are connected by
an edge from the root.
BINARY TREES
Definition
A binary tree is a tree in which no node can have no more than two children. Because there are only two children, we
can name them left and right.
Definition (Recursive definition of a binary tree)
A binary tree is a finite set of elements that is either:
- empty; or
- is partitioned into 3 disjoint subsets, which are the following:
- a subset containing a single element called the root of the tree
- a left subtree
- a right subtree
Applications of binary trees:
- expression tree
- Huffman coding tree
- binary search trees
BINARY SEARCH TREES
Definition
The binary search tree is a binary tree that satisfies the search order property. That is, for every node X in the tree, the
values of all keys in the left subtree are smaller than the key in X and the values of all keys in the right subtree are larger
than the key in X. Duplicate keys are not allowed.
Example:
Asian College of Technology
College of Computer Studies
Traversing a Binary Search Tree
1. Inorder traversal
Visit the left subtree, then the node, then the right subtree.
Algorithm:
• If there is a left child visit it
• Visit the node itself
• If there is a right child visit it
2. Preorder traversal
Visit each node then visit its children.
Algorithm:
• Visit the node itself
• If there is a left child visit it
• If there is a right child visit it
3. Postorder traversal
Visit each node after visiting its children.
Algorithm:
• If there is a left child visit it
• If there is a right child visit it
• Visit the node itself
Finding a Node and Minimum/Maximum Values in a Binary Search Tree
The code for finding the minimum and maximum values is almost trivial in both cases, due to the properties of a BST.
The smallest value in a BST will always be found at the last left child node of a subtree beginning with the left child of
the root node. On the other hand, the largest value in a BST is found at the last right child node of a subtree beginning
with the right child of the root node.
Inserting and Deleting a Node into the BST
To insert:
• If value we want to insert < key of current node, we have to go to the left subtree
• Otherwise we have to go to the right subtree
• If the current node is empty (not existing) create a node with the value we are inserting and place it here.
Asian College of Technology
College of Computer Studies
For example, inserting ’3’ into the BST?
6 3
3 5
4 10
7 1
2 5 7 9
4 7
Inserting node 3 into the BST
To delete:
• Insertion begins as a search would begin; if the root is not equal to the value, we search the left or right subtrees
as before. Eventually, we will reach an external node and add the value as its right or left child, depending on
the node's value. In other words, we examine the root and recursively insert the new node to the left subtree
if the new value is less than the root, or the right subtree if the new value is greater than or equal to the root.
• When we delete a node, we need to consider how we take care of the children of the deleted node.
• This has to be done such that the property of the search tree is maintained.
Deletion under Different Cases
Case 1: the node is a leaf
- Delete it immediately
Case 2: the node has one child
- Adjust a pointer from the parent to bypass that node
Asian College of Technology
College of Computer Studies
Case 3: the node has 2 children
– Replace the key of that node with the minimum element at the right subtree
– Delete that minimum element
– Has either no child or only right child because if it has a left child, that left child would be smaller and
would have been chosen. So, invoke case 1 or 2.
EXPRESSION TREE
An expression tree for an arithmetic, relational, or logical expression is a binary tree in which:
• The parentheses in the expression do not appear.
• The leaves are the variables or constants in the expression.
• The non-leaf nodes are the operators in the expression:
• A node for a binary operator has two non-empty subtrees.
• A node for a unary operator has one non-empty subtree.
• The operators, constants, and variables are arranged in such a way that an inorder traversal of the tree
produces the original expression without parentheses.
Asian College of Technology
College of Computer Studies
Expression Tree Examples
Inorder Traversal
Expression Expression Tree
Result
(a+3) a+3
a 3
log
log(x) log x
n! n!
Why Expression Tree?
• Expression trees are used to remove ambiguity in expressions.
• Consider the algebraic expression 2 - 3 * 4 + 5.
• Without the use of precedence rules or parentheses, different orders of evaluation are possible:
((2-3)*(4+5)) = -9
((2-(3*4))+5) = -5
(2-((3*4)+5)) = -15
(((2-3)*4)+5) = 1
(2-(3*(4+5))) = -25
• The expression is ambiguous because it uses infix notation: each operator is placed between its operands.
• Expression trees can be very useful for:
• Evaluation of the expression.
• Generating correct compiler code to actually compute the expression's value at execution time.
• Performing symbolic mathematical operations (such as differentiation) on the expression.
Asian College of Technology
College of Computer Studies
Hi, Flashee here!
Write your insights about this lesson. Congrats buddy, you have finished Lesson 5 – TREES. Are you now ready
to do the lesson activity?
WORKSHEET IN LESSON 5: TREES
Student’s Name: Section:
Instructor’s Name: Date Submitted:
General Instructions:
• This worksheet comprises different activities based on the different topics in this lesson.
• Separate instructions are given to each topic activity in this worksheet. Read them carefully.
• Once done with each activity on this worksheet, kindly take a picture/s of your answered activity in this
worksheet.
• Submit the image of each answered activity to our agreed submission portal.
Activity 5.1 – EXPRESSION TREE
Directions: Date & Time Answered:
• Given the expression, create the equivalent expression tree. Determine the
traversals. Score:
• Place your answer into a document then save it into a PDF format.
• Submit it to our designated submission portal.
1. (2+3) * 6 – 2
2. a + h * m + k – l / x
Asian College of Technology
College of Computer Studies
Activity 5.2 – BINARY TREE
Directions: Date & Time Answered:
• Do as told.
• Place your answer into a document then save it into a PDF format. Score:
• Submit it to our designated submission portal.
1. Use the following binary tree to write out each of the three traversals indicated below.
Preorder traversal ______________________________________________
Inorder traversal _______________________________________________
Postorder traversal _____________________________________________
2. Construct the binary tree for the following keys: 99, 6, 20, 32, 7, 101, 1, 5
Asian College of Technology
College of Computer Studies