0% found this document useful (0 votes)
33 views42 pages

Data Structure - Lec-5

Uploaded by

Mahmoud Mousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views42 pages

Data Structure - Lec-5

Uploaded by

Mahmoud Mousa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Trees & Binary Trees

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

Tree Binary Tree


4
Tree Terminology
• Parent or predecessor: the node directly above in the
hierarchy
• Child or successor: a node directly below in the hierarchy
• Siblings: nodes that have the same parent
• Ancestors of a node: its parent, the parent of its parent,
etc.
• Descendants of a node: its children, the children of its
children, etc.
• Size of a tree: is the number of nodes.

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

• Degree or arity of a tree: the maximum of the degrees


of the tree’s nodes

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

• A binary tree is a positional tree, i.e. it matters


whether the subtree is left or right

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

Depth-first traversal of a binary tree

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

• Recursive algorithm for pre-order traversal:


• If tree is not empty,
− Visit root node of tree
− Perform pre-order traversal of its left subtree
− Perform pre-order traversal of its right subtree

20
Pre-order Traversals
A

B C

D E F G

H I

Pre-order Traversal: AB DHE CF I G


21
In-order Traversals
• Start at the root
• Visit the left child of each node, then the node, then any
remaining nodes

• Recursive algorithm for in-order traversal


• If tree is not empty,
− Perform in-order traversal of left subtree of root
− Visit root node of tree
− Perform in-order traversal of its right subtree

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

• Recursive algorithm for post-order traversal


• If tree is not empty,
− Perform post-order traversal of left subtree of root
− Perform post-order traversal of right subtree of root
− Visit root node of tree

24
Post-order Traversals
A

B C

D E F G

H I

In-order Traversal: HDE B I F GCA


25
Breadth-first Traversal
Level-order (breadth-first) traversal: where we visit every node on a level
before going to a lower level.
The Figure shows how we visit each node in a tree using breadth-first traversal.
The figure also shows the walking order.

The traversal order is : A, B, E, C, D, F.


26
Discussion
• Note that the relative order of the recursive calls in the
pre-order, in-order and pos-torder traversals is the same
• The only differences stem from where the visiting of the
root node of a subtree actually takes place
• Which means in all the depth-first traversals, we visit left
before right, what differs is visiting the node itself.

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,

Preorder traversal sequence: F, B, A, D, C, E, G, I, H

Inorder traversal sequence: A, B, C, D, E, F, G, H, I

o Note that the in-order traversal of a binary search tree yields an ordered list.

Postorder traversal sequence: A, C, E, D, B, H, I, G, F

Level-order traversal sequence: F, B, G, A, D, I, C, E, H

32
Operations on a Binary Tree
• What might we want to do with a binary tree?
− Add an element (but where?)

− Remove an element (but from where?)

− Find an element in the tree

− Traverse the tree (in preorder, inorder, postorder, level order)

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

2 25 45 Since left is null so


insert 20 on 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

Replacing 10 Replacing 5 Deleting leaf


with largest with largest
value in left value in left
subtree subtree
39
Example Deletion (Internal node)
 Delete ( 10 )
10 25 25

5 30 5 30 5 30

2 25 45 2 25 45 2 45

Replacing 10 Deleting leaf Resulting tree


with smallest
value in right
subtree
40
Linked Binary Tree Implementation
• To represent the binary tree, we will use a linked structure
of nodes
− root: reference to the node that is the root of the tree

− count: keeps track of the number of nodes in the tree

• First, how will we represent a node of a binary tree?

41
Binary Tree Node
• A binary tree node will contain
− a reference to a data element
− references to its left and right children

left and right children are binary tree nodes themselves

42

You might also like