Data Structure - Lec-5
Data Structure - Lec-5
Tree Terminology
Interior Root
nodes
Edges
Leaf
nodes
2
Tree Terminology
• Nodes: the elements in the tree
• Edges: connections between nodes
• Root: the distinguished element that is the origin of the tree
− There is only one root node in a tree
• Leaf node: a node without an edge to another node
• Interior node: a node that is not a leaf node
• Empty tree has no nodes and no edges
3
Tree Terminology
Tree
− Nodes
− Each node can have 0 or more children
− A node can have at most one parent
Binary tree
− Tree with 0–2 children per node
5
Height of the tree
• A path is a sequence of edges leading from one node to
another
• Length of a path: number of edges on the path
• The height of a node is the length of the longest
downward path to a leaf from that node.
• Height of a (non-empty) tree : is length of the longest
path from the root to a leaf. (height of root)
− What is the height of a tree that has only a root
node? The height is 0
− By convention, the height of an empty tree is -1
6
Depth of the tree (level)
• Level of a node (depth): is the length of the path to its
root, that is the number of edges between root and
node. Also can be defined as number of ancestors of a
node.
• It can be defined recursively:
− Level of root node is 0
− Level of a node that is not the root node is level of its
parent + 1
• Question: What is the height of a tree in terms of levels?
The height of the tree is the maximum level of a leaf
7
Level of a node
Level 0
Level 1
Level 2
Level 3
8
Subtrees
Subtrees of
the root node
9
Subtrees
E
Subtrees of the
node labeled E
10
More Tree Terminology
• Degree or arity of a node: the number of children it has
11
Example
Refer to the following tree to answer the questions bellow:
Q-A:
What is the height of this tree?
Answer: 4
Q-B:
How many descendants does node J have?
Answer: 4
Q-C:
What is the size of this tree?
Answer: 17
Q-E:
What is the degree of this tree?
Answer: 5 ; the degree of a tree is the maximum degree of any of its nodes, and C has 5 nodes .
Q-F:
How many nodes are at level 2?
Answer: 8
Q-G:
What is the largest possible height of any tree of degree 3 and size of 100?
Answer: 97
12
Binary Trees
• General tree: a tree each of whose nodes may have any
number of children
• n-ary tree: a tree each of whose nodes may have no more
than n children
• Binary tree: a tree each of whose nodes may have no more
than 2 children
− i.e. a binary tree is a tree with degree (arity) 2
− The children (if present) are called the left child and
right child
13
Binary Tree-Example
A
B C
D E F G
H I
14
Binary Trees
• Recursive definition of a binary tree:
it is
− Either an empty tree
− Or, a tree which has a root whose left and right subtrees
are binary trees
15
Full Binary Tree
A full binary tree is a tree in which every node other
than the leaves has two children.
16
Complete Binary Tree
A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes
are as far left as possible (left is filled before right).
17
Tree Traversal
A binary tree traversal requires that each node of the tree be processed once
and only once in a predetermined sequence. The two general approaches to
the traversal sequence are depth-first and breadth-first traversal.
The 3 forms of visiting these nodes (Depth-first) are:
a. Pre-order traversal
b. In-order traversal
c. Post-order traversal
18
Depth-First Traversals
A
B C
D E F G
H I
19
Pre-order Traversals
− Start at the root
− Visit each node, followed by its children;
− Visit left child before right
20
Pre-order Traversals
A
B C
D E F G
H I
22
In-order Traversals
A
B C
D E F G
H I
In-order Traversal: D H B E A I FC G
23
Post-order Traversals
• Start at the root
• Visit the children of each node before visiting the node
24
Post-order Traversals
A
B C
D E F G
H I
27
Example 1:
Find the pre-order, post-order and in-order traversal of the following
binary tree.
Answer:
Pre-order:14,84,13,53,16,99,72,43,33,64,97
Post-order:53,13,99,72,16,84,64,33,97,43,14
In-order:13,53,84,99,16,74,14,33,64,43,97
28
Example 2:
Construct a tree knowing that the following are the pre-order and
the in-order traversal of it.
Pre-order: 4, 3, 2, 1, 8, 6, 5, 7, 9
In-order: 1, 2, 3, 4, 5, 6, 7, 8, 9
29
Binary Search Tree
Key property
▪ Value at node
−Smaller values in left subtree
− Larger values in right subtree
▪ Example
X
− X>Y
− X<Z
Y Z
30
Binary Search Tree
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary search Not a binary
trees search tree
31
Example
In this binary search tree,
o Note that the in-order traversal of a binary search tree yields an ordered list.
32
Operations on a Binary Tree
• What might we want to do with a binary tree?
− Add an element (but where?)
33
Insertion to a BST
To add a new node N to a BST:
Start at root Node (current = root)
if N is before current and current left child is null
insert N as left child
else if N is after current and current right child is null
insert N as right child
else if N is before current
repeat the same algorithm for left subtree
(current=current.left)
else
repeat the same algorithm for right subtree
(current=current.right)
34
Insertion to a BST
Insert ( 20 )
10 10 < 20, go to right
30 > 20, go to left
5 30
25 > 20, go to left
20
35
Remove from a BST
To delete a node with value X from a BST
Perform search for value X
If X is a leaf, delete X
Else
a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree
36
Remove from a BST
• Example:
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
37
Example Deletion (Leaf)
Delete ( 25 )
10 10
10 < 25, right
5 30 30 > 25, left 5 30
25 = 25, delete
2 25 45 2 45
38
Example Deletion (Internal node)
Delete ( 10 )
10 5 5
5 30 5 30 2 30
2 25 45 2 25 45 2 25 45
5 30 5 30 5 30
2 25 45 2 25 45 2 45
41
Binary Tree Node
• A binary tree node will contain
− a reference to a data element
− references to its left and right children
42