Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 49

Chapter 8: Binary Trees

Introduction
A tree consists of a nonempty finite set
of elements called nodes (vertices) and
a finite set of directed edges or lines
called branches that connect these
nodes.
A tree is an acyclic directed graph (does
not contain any cycles – never return to
starting point).
Common Tree Terminologies
Node (vertex) – Element in tree which
contains information and link to other node in
tree.
Branches– Connections between nodes
(vertices) that represents some relationship
associated with the nodes.
Parent – A node is a parent if it has one or
more children ( has out-degree more than
zero).
Common Trees Terminologies
Siblings – Children with the same
parent node.
Children – If N is a node with
successors X and Y, then X and Y are
the children of N.
Terminal node/leaf – A node that has
no children. A terminal node is also
called a leaf.
Common Trees Terminologies
Ancestor – All the nodes along the path from
the root to that node.
Descendant – All the nodes that are in its
subtree.
Traversal – Visiting the nodes of tree.
In-degree – The number of arrow or lines
coming into a node.
Out-degree – The number of arrow or lines
going out of a node.
Common Trees Terminologies
Depth of node – The number of steps
taken when moving upward from a
node to its parent.
Height – The number of branches from
the root to its farthest descendant.
Binary Tree Example
A

B C

D F G
E

H I K
J

A tree with 12 nodes


Tree Example (cont…)
Node A is the root of the tree.
Node A has two children, B and C.
There is a branch from node A to node
B, where A is called the parent of B and
B is called the child of A.
Each of nodes B, C, D and F has two
children
Tree Example (cont…)
Node K has only one child.
The nodes H, I, E, J, L and G have no
children. These nodes without any
children are called terminal nodes or
leaves.
Nodes with the same parents are called
siblings. D and E are siblings since they
have B as their parent.
A

B C

Exercise H
D
I
E F G

J K
L

Name the relationship/terminologies for


the following nodes:
F&K
D&E
A
L
Binary Tree
A binary tree contains a finite set of
nodes. Characteristics of binary tree are:
Each node has exactly one parent (in-degree
of one). Only the root node has no parent
(in-degree of zero).
Each node may have up to two children,
called the left child and the right child. Each
parent in binary tree may has 0, 1 or
maximum of 2 children. That why the word
“binary” is used.
Binary Tree
There are no cycles in the binary tree.
Moving from a child node upwards to its
parent and continue moving up to the
parent of parent, and so forth will
eventually reach the root.
Binary Tree Implementation
Binary tree can be represent using the
linked representation

Struct btree_node
{
int data; // Serves as data field
struct btree_node *left_child;// Points to left subtree
struct btree_node *right_child;// Points to right subtree
};
Binary Search Tree (BST)
Every non empty BST contain a value
(usually an integer but can be a string)
and satisfies the following conditions:
Every node or element has a value and no
two nodes in the BST has equal value.
Its left child (if exist) contain value smaller
than parent value.
Its right child (if exist) contain value larger
than parent value.
Binary Search Tree (BST)
Binary search tree begin by comparing
the search key with the data value
stored in root node.
If it matches the root key, the search is
over.
If no match is found, the search key is
determine whether it is less than or
greater than root node.
Binary Search Tree (BST)
If the search key is less than the root
value, the searching continue with the
left subtree or otherwise searching will
continue with the right subtree.
The searching is done recursively until
the key value is found.
Searching In Binary Tree
6388

2154 7100

6838 7133
3602
877

Searching 877 in Binary Search Tree


Binary Search Tree Algorithm
Suppose T is a binary search tree and
the search key is ITEM
1. Compare ITEM with the value at root
node N of the tree.
• 1.1. If ITEM is not equal to the key value of
root node N of the tree
– 1.1.1. If ITEM<N, search the left child of N.
– 1.1.2. Else if ITEM>N, search the right child of N.
2. Repeat step 1 until a match is found. If
no match is found, terminate the loop.
Inserting Binary Search Tree
Insertion into a binary search tree is
quite similar to searching.
Insert into a binary search tree is done
after the correct node location is found.
The new node after the insertion
becomes a leaf in the tree.
Inserting Binary Search Tree
6388

2154 7100

877 6838 7133


3602

7500

Inserting 7500 to Binary Search Tree


Inserting Binary Search Tree
Compare 7500 with root 6388. Since 7500>6388, try
insert 7500 in the root’s right subtree.
Since the subtree is not empty, compare 7500 with
that subtree’s root of 7100. Since 7500>7100, try
insert 7500 in that subtree’s right subtree.
Since the subtree (the one with 7133 as its node) is
not empty, compare 7500 with that subtree’s root.
Since 7500>7133, try insert 7500 in that subtree’s
right subtree.
Since that subtree is empty, insert 7500 as the root
of that subtree. This node also become a leaf this
binary tree.
Exercise
Redraw the picture and insert 7130 and 2555 into the
binary tree
6388

2154 7100

877 6838 7133


3602

7500
Inserting BST Algorithm
Assume binary tree T and NEWITEM to
insert.
1. Compare NEWITEM with the key value at
root node N of the tree.
• 1.1 If NEWITEM<N, proceed to the left child of N.
• 1.2 If NEWITEM>N, proceed to the right child of N.
2. Repeat step 1 until an empty place to insert
NEWITEM is found.
Deleting Binary Search Tree
Deleting a node from a binary tree
depends on how many children the
node ( to be deleted) has. There are
three cases of deletion:
Deletion of node without children.
Deletion of node with only one subtree or
child.
Deletion of node with two subtrees or
children.
Deletion of node without children
For a node without children, deletion is
done without further adjustment to the
tree.
Adjust the link field of parent to null
and remove the child from the tree.
Deletion of node without children
6388

2154 7100

877 6838 7133


3602

7500

Deleting 6838 from Binary Tree


Deletion of node without children
Compare 6838 with root 6388. Since
6838>6388, recursively move to the right
subtree.
Compare 6838 with the value of the root
there (7100). Since 6388<7100, move to left
subtree.
Compare 6838 with value in the root node
there (6838) and the match is found. Set the
7100’s left pointer to NULL and delete the
node containing 6838.
Deletion Of Node With Only One Subtree Or Child

6388

2154 7100

7133
877 6838
3602 7500

Deleting 7133 from Binary Tree


Deletion Of Node With Only One Subtree Or Child

Start by comparing 7133 to the value in the root


node (6388). Since 7133 > 6388, move to right
subtree.
Compare 7133 with the value in the root node there
(7100), Since 7133>7100, move to right subtree.
Compare 7133 with the value in root node there
(7133) and the match is found.
7133 has no left subtree but has a right subtree. Set
the 7100’s left pointer to point the right subtree of
7133 and delete the node containing 7133.
Deletion Of Node With Two Subtrees Or
Children

6388

2154 7100

877 6838
3602 7500

Deleting 6388 from Binary Tree – Before deletion


Deletion Of Node With Two Subtrees Or
Children

3602

2154 7100

877 6838
7500

Deleting 6388 from Binary Tree – After deletion


Deletion Of Node With Two Subtrees Or Children

The order of binary tree must be preserve


when deleting node that has two children.
This is done by replacing the node to be
deleted with its immediate successor.
The immediate successor of a node is either
the rightmost descendent of the node’s left
subtree or the leftmost descendent of the
node’s right subtree.
Binary Search Tree Deletion
1. Compare DELITEM with the key
value at root node N of the tree.
1.1 If DELITEM < N, search the left child of
N.
1.2 Else if DELITEM > N, search the right
child of N.
2. Repeat step 1 until the matches is
found or not.
Binary Tree Deletion Algorithm
2.1 If DELITEM is found, determine whether
DELITEM contains any child or not.
• 2.1.1 If DELITEM has no child, adjust the pointer of
DELITEM’s parents to point to null and delete DELITEM
from the tree and exit.
• 2.1.2 Else determine whether DELITEM got one child or
two children.
– 2.1.2.1 If DELITEM got one child, delete DELITEM and
move its only child up to take its place.
– 2.1.2.2 If DELITEM has two children, preserve the order of
the tree by replacing DELITEM with its immediate
successor. The immediate successor of a node is the
rightmost descendent of the node’s left subtree.
2.2 Else if DELITEM is not found, abort deletion
and exit.
Traversing A Binary Tree
Traversing a binary tree involves systematically
“visiting” each node in the tree in some particular
order.
Visiting a node means doing something with the
node such as using the value of the node, update the
value, printing it out, etc.
In traversing a binary tree, each node of the tree will
be processed or reached by traversing links starting
from the root of the tree.
There are two general approaches for traversing a
binary tree:
Breadth first traversal
Depth first traversal
Breadth First Traversal
In the breath first traversal, all adjacent
nodes are processed before moving to
process the descendants of a node at
the next level.
Start at level 0 and then processes all
the nodes in level 1 before going on to
process the nodes in level 2.
Breadth First Traversal
The processing proceeds horizontally from
the root to all of its children at next level,
then to its children’s children at the next level
and so forth until all nodes at each level have
been processed.
Each level is completely processed before
moving on to process the next level and no
node is processed more than once.
Breadth First Traversal
A

B C

D F G
E

Breadth first traversal of a tree


Depth First Traversal
In the depth first traversal, all node’s
descendents are processed before moving to
process an adjacent node.
The processing proceeds along a path from
the root through one child to the most distant
(at furthest level) descendent of that first
child before processing a second child.
All descendents of a child are processed
before the next child.
Depth First Traversal
There are mainly three different depth-first
traversal sequences:
Preorder traversal (NLR) – visit the node before
visiting any of its children.
Inorder traversal (LNR) – visit the left subtree of
the node before visiting the node.
Postorder traversal (LRN) – visit the left and right
subtree of the node before visiting the node.
Preorder Traversal - NLR
The order of visitation of nodes is “root, left,
right” (Node, Left branch and Right branch –
NLR)
The steps are as follows:
Starting at the root, first access the node (root)
itself.
Then traverse the node’s left branch (left subtree)
in preorder.
And finally traverse the node’s right branch (right
subtree) in preorder.
Preorder Traversal - NLR
NLR : A B D E C F G A

B C

D F G
E

Processing order for Preorder Traversal


Exercise
Traverse the binary tree by using the
Preorder Traversal (NLR)
4456

2154 7133

6838 8386
877 3602

6388 7100 7500


Inorder Traversal - LNR
The order of visitation of nodes is “left , root,
right” (Left branch, Node and Right branch –
LNR)
The steps are as follows:
Starting at the root, first access each node’s left
branch (left subtree) in inorder.
Then the node itself (root).
And finally traverse the node’s right branch (right
subtree) in inorder.
Inorder Traversal - LNR
LNR : D B E A F C G A

B C

D F G
E

Processing order for Inorder Traversal


Exercise
Traverse the binary tree by using the
Inorder Traversal (LNR)
4456

2154 7133

6838 8386
877 3602

6388 7100 7500


Postorder Traversal - LRN
The order of visitation of nodes is “left, right,
root” (Left branch, Right branch and Node, –
LRN)
The steps are as follows:
Starting at the root, first access each node’s left
branch (left subtree) in postorder.
Then traverse the node’s right branch (right
subtree) in postorder.
And finally the node itself.
Postorder Traversal - LRN
LRN : D E B F G C A A

B C

D F G
E

Processing order for Postorder Traversal


Exercise
Traverse the binary tree by using the
Postorder Traversal (LRN)
4456

2154 7133

6838 8386
877 3602

6388 7100 7500

You might also like