Chapter 7
Chapter 7
Tree
CH-7 Contents
6/1/2021 2
Trees
6/1/2021 3
Trees
6/1/2021 4
Some Tree Terminologies
Child and parent
Every node except the root has one parent (J is a parent of P and Q)
P and Q are child of J
A node can have an arbitrary number of children (A has 6 while D has 1 child)
Leaves/External Nodes
Nodes with no children (B, C, H, I, P, Q, K, L, M, N)
Sibling
nodes with same parent (Example, P and Q)
Internal node
A node with at least one child (A,D,E,F,G,J)
Degree: the number of possible child of a node
Degree of node F = 3, node N=0, degree of the tree=degree of node having
maximum degree. (=6) 6/1/2021 5
Some Terminologies
▪ Path − Path refers to the sequence of nodes along the edges of a tree.
– Path length: the number of edges that must be traversed to get from one node
to another
Length
Number of edges on the path from node x to node y
Depth of a node
Number of edges from the root to that node (Depth of C =1, of A = 0)
The depth of a tree is equal to the depth of the deepest leaf (=3)
Because, the deepest are P and Q and their depth is 3
6/1/2021 6
Some Terminologies
Level
Level of node n, is the depth of node n.
The level of a node is one greater than the level of its parent.
Height of a node
length of the longest path from that node to a leaf (E=2)
all leaves are at height 0
The height of a tree is equal to the height of the root
Ancestor and descendant
The ancestors of a node are all the nodes along the path from the root to the
node.
Parent, grand parent and great grand parents of anode
Descendant of a node are nodes reachable by repeated proceeding from
parent to child.
Child, grand child and great grand child of a node 6/1/2021 7
Applications of Trees
6/1/2021 8
Example: UNIX Directory
▪ Tree is useful to represent hierarchical data
▪ One of its application a file system used by many systems
▪ The following is an example of unix file system
6/1/2021 9
Exercise: Given Tree
6/1/2021 10
Binary Tree
6/1/2021 11
Binary Tree
▪ A general tree is a tree where each node may have zero or more
children (a binary tree is a specialized case of a general tree).
– General trees are used to model applications such as file systems.
▪ Binary tree: each node has at most two children
– The possible children are usually referred to as the left child and the right
child
A
– A unique path exists from the root to every other node
B C
Applications:
arithmetic expressions D E F G
decision processes
H I
searching 6/1/2021 12
Complete Binary Tree
13
Full Binary Trees
▪ A full binary tree is a binary tree in which every node has either zero
or two children. That is, no nodes have only one child.
6/1/2021 14
Perfect Binary Trees
• A perfect binary tree is one that is both full and complete.
• All leaf nodes will be at the same level, and this level has the maximum number
of nodes.
6/1/2021 15
Exercise: Say True/False Based on figure a and b
16
Binary Tree: # of nodes
6/1/2021 17
Why height (H) is important?
▪ What is the total # nodes N of a perfect binary tree with height H?
– perfect binary tree: Every node has exactly two children and all the leaves
are on the same level.
– N= 2(H + 1) -1
▪ What is the height H of a perfect binary tree with N nodes?
– H= log(N +1) - 1 O(Log N)
6/2/2021 18
•What is the max height of a tree with N nodes?
N (same as a linked list)
6/1/2021 19
Tree traversal
▪ Traversing a tree means processing it in such a way that each node is visited for processing only once.
▪ There are three traversals methods used to visit/print out the node/data in a tree in a certain order
▪ Pre-order traversal
– Print the data at the root
– Recursively print out all data in the left subtree
– Recursively print out all data in the right subtree
▪ In-order Traversal
– Recursively print out all data in the left subtree
– Print the data at the root
– Recursively print out all data in the right subtree
▪ Post-order Traversal
– Recursively print out all data in the left subtree
– Recursively print out all data in the right subtree
– Print the data at the root
6/1/2021 20
Traversal Applications
▪ Make a clone
▪ Determine height
▪ Determine number of nodes
▪ representing arithmetic expression
6/2/2021 21
More on Tree traversal
▪ You may 'pass through' a node as many times as you like but you can only
process the node once.
▪ During a pre-order traversal each node is processed before any nodes in its
subtrees
▪ During an in-order traversal each node is processed after all nodes in its left
subtree but before any nodes in its right subtree.
▪ During a post-order traversal each node is processed after all nodes in both its
subtrees
6/1/2021 24
Preorder, Postorder and Inorder
▪ Once the expression tree is built, all the three forms of an algebraic expression
(infix, prefix, and postfix) are immediately available to us if we know exactly how
the corresponding tree should be traversed.
▪ Preorder traversal
– node, left, right (recursively)
– It produces prefix expression
▪ ++a*bc*+*defg
6/1/2021 25
Preorder, Postorder and Inorder
▪ Post-order traversal
– left, right, node (recursively))
– Gives postfix expression
▪ abc*+de*f+g*+
▪ In-order traversal
– left, node, right. (recursively)
– Gives infix expression
▪ a+b*c+d*e+f*g
6/1/2021 26
More on tree Traversal
6/2/2021 27
More on tree traversal
6/2/2021 28
Exercise 1
▪ Show list of nodes when the following tree is traversed in: the tree in
6/1/2021 29
Exercise 2
6/2/2021 30
How to search a binary tree?
Question
– Is this better than searching a linked list?
Answer
– No, it takes a linear time O(N)
6/1/2021 31
Binary Search Tree(BST)
6/1/2021 32
Binary Search Tree (BST)
6/1/2021 33
BST Representation
6/1/2021 34
Binary Search Trees
6/1/2021 35
Implementing binary trees
1. Trees, binary trees and binary search trees
2. Binary Search tree operation
1. Insertion
2. Deletion
3. searching
4. Traversal
3. Balancing a tree
6/1/2021 36
Implementing Binary Search Tree
6/1/2021 39
Linked representation of binary search
trees
▪ Because each node in a binary tree may have two child nodes, a node in a linked
representation has two pointer fields
▪ We will use the following general structure specification
struct Node
{
int data; //the data type can be any appropriate type
node *left;
node *right;
};
Node *root = NULL; // The root node of the tree
6/1/2021 40
BST Basic Operations
6/1/2021 41
Searching BST
6/1/2021 42
6/1/2021 43
Searching (Find)
Find X: return a pointer to the node that has key X, or NULL if there is no such
node
Node*findMax(node*root)
{
If(root==NULL)
Return Null;
Else if(root->right==Null)
Return root
Else
Return(findMax(root->right)
} 6/1/2021 46
Inserting node in BST
▪ When a new node is inserted the definition of BST should be preserved.
▪ There are two cases to consider
– There is no data in the tree (root=null)
▪ root=newnode;
– There is data
▪ Search the appropriate position
▪ Insert the node in that position.
6/1/2021 47
Example-insert node13
▪ Proceed down the tree as you would with a find
▪ If X is found, do nothing (or update something)
▪ Otherwise, insert X at the last spot on the path traversed
6/1/2021 48
Time complexity: O(height of the tree)
Insert node
void insertNode( Node &root, pnode p)
{
if(root==NULL)
{
root=p;
root->left=NULL;
root->right=NULL;
}
else if(root->data>p->data)
add(root->left,p);
else
add(root->right,p);
}
6/1/2021 49
Deletion: BST
6/1/2021 53
Case 1
6/1/2021 54
Case 2
6/1/2021 55
Case 3
6/1/2021 56
Traversal: Binary Search Tree
6/1/2021 58
Algorithm: In order Traversal
6/1/2021 59
Algorithm: Post order Traversal
6/1/2021 60
Algorithm: Level order Traversal
▪ BSTs where introduced because in theory they give nice fast search
time.
– Example1:
▪ Design a BST using the following sequence of data: 6, 4, 8, 5, 7, 3, 9,10
– Example2:
▪ Design a BST using the following sequence of data: 3, 4, 5, 6, 7, 8 ,9 ,10
▪ We have seen that depending on how the data arrives the tree can
degrade into a linked list
▪ So what is a good programmer to do.
– Of course, they are to balance the tree.
6/1/2021 62
Next Time!!
6/1/2021 63