0% found this document useful (0 votes)
140 views55 pages

Chap 7 Trees

This document provides an overview of trees and tree-related concepts. It begins with objectives and content for the chapter on trees. It then defines basic tree terminology like root, leaf nodes, ancestors, descendants, etc. It introduces different types of trees like binary trees and expression trees. It explains how to build and traverse binary trees using preorder, inorder and postorder traversal. It also covers searching and operations in binary search trees like insertion, deletion, and implementations of a BST class.

Uploaded by

Bhavin Panchal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
140 views55 pages

Chap 7 Trees

This document provides an overview of trees and tree-related concepts. It begins with objectives and content for the chapter on trees. It then defines basic tree terminology like root, leaf nodes, ancestors, descendants, etc. It introduces different types of trees like binary trees and expression trees. It explains how to build and traverse binary trees using preorder, inorder and postorder traversal. It also covers searching and operations in binary search trees like insertion, deletion, and implementations of a BST class.

Uploaded by

Bhavin Panchal
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 55

Chapter 7: Trees

Objectives
Basic Concepts and Terminology
Trees Type
Build a Tree
Trees Operation

Content
Introduction
Binary tree
Expression tree
Traversal in Binary Tree
Searching in Binary Tree
AVL Tree
B-Trees

Introduction
A tree consists of a finite set of elements, called nodes, and a

finite set of directed lines, called branches, that connect the


node.
Trees can be classify as:
Static Trees the form of trees has been determined.
Dynamic Trees the form of trees is varying during the execution.

Fig. 1: A tree

Introduction
Basic Tree Anatomy
Information from a tree are:
Family Relationship parent node & child node.
Geometric Relationship left /right/bottom/up.
Biological Name for tree root, leaves, internal

node, level.

Introduction
Basic Tree Anatomy
The first node is called the root.
A node is a parent if it is has

successor nodes.
A node with predecessor is a child.
A child node can be a left child node (left sub
tree) or right child node (right sub tree).

Two or more node with the same

parent are siblings.


A leaf node is a node without any
child or empty sub trees.
Nodes that are not a root or leaf are
known as internal nodes because
they are found in the middle portion
of a tree.
An ancestor is any node in the path
from the node to the root.
A descendant is any node in the
path below the parent node; that is,
all nodes in the paths from a given
node to a leaf are descendants of the
node.

BINARY TREES
Binary tree is a tree in which no node can have more

than two subtrees (a node can have zero, one, or two


subtrees).

A null tree is a tree with no node (see Fig. 4(a))


As you study this figure, note that symmetry is not a tree

requirement.

BINARY TREES
Example
Children

Parents

20
2
4
8

:
:
:
:

:2
: 20

2
Descendant
7

2, 8
4, 7
9, 1
3

Siblings
2&8

: 4, 9, 1,

: 4, 2,

: 9&1, 4&7,

Ancestor
20

Level 0

20

Level 1

Level 2

Level 3

Traversing
Example
In general, any nodes, N can be accessed by traversing the tree in the path,

P start from root node. If the path, P consists of n, therefore node N is


located in the nth level and the path, P length will be n.
Path to node 9, starting from root. There are three paths; 20 2, 2 4, 49.
Therefore the node is located in 3rd level and the length of the path is 3.
The height of binary tree = maximum level + 1.
Complete Binary tree is a tree of which their leaves are at the same level
and each node will have two children or none.

Complete Tree
Example
A complete tree has the maximum number of entries for its

height.
To proof the complete binary tree:
Consider that the height of tree is K.
It will contain;
The number of Node: 2k - 1
The number of Leaves: 2k-1

Note
In trees, there is only

one path from root to


each descendant. If
there is more than one
path, therefore the
diagram is not a tree.

Let say K = 4.
A diagram with their
Therefore, the number of nodes:
path in a circle and two
different paths to a node
24 1=16 -1=15 nodes.
but reconnected at
The number of leaves:
another node is also not
24-1 = 23 = 8 leaves.
a tree.
A Skewed Binary tree is a tree with only left or right children.

EXPRESSION TREE
Expressions tree is an application of binary tree.
Arithmetic expression is represented by a tree.
An expression tree is a binary tree with the

following properties:
1. Each leaf is an operand.
2. The root of an internal nodes are operators. (+,

-, *, /)
3. Subtrees are subexpressions with the root being
an operator.

EXPRESSION TREE
EXAMPLE
(X + Y) * (A - B) can be represented as:
*

The parentheses (( and )) is omitted but

stated by the nature of the tree.

EXPRESSION TREE
EXAMPLE
X * (Y / -Z)

EXPRESSION TREE
BUILD AN EXPRESSION - EXAMPLE
(A - B + C) * (-D)

BINARY TREE TRAVERSALS


A binary tree traversal requires that each node of tree be processed

once and only once in predetermined sequence. There are 3 possible


methods:

Pre-order @ prefix

In-order @ infix

Post-order @ postfix

root - left
right

left - root
right

left - right
root

visit root
traverse left
subtrees
traverse right
subtrees

traverse left
subtrees
visit root
traverse right
subtrees

traverse left
subtrees
traverse right
subtrees
visit root

BINARY TREE TRAVERSALS


EXAMPLE
G

Preorder (root - left - right)


G D B A C E F K H J I M L
Inorder (left - root - right)
A B C D E F G H I J K L M

Postorder (left - right - root)


A C B F E D I J H L M K GA

BINARY TREE
TRAVERSALS
Preorder (root - left - right)
EXAMPLE
* + a - b c /

- de- + f gh

+
Inorder (left - root - right)
[a + (b c)] * [(d e) / (f + g a h)]
b

Postorder (left - right - root)


a b c - + d e - f g + h - / *

*
/
-

+
f

h
g

BINARY SEARCH TREE


Binary Search Tree (BST) is a tree with the

following properties:
1. All items in the left subtree are less than the

root.
2. All items in the right subtree are greater than
or equal to the root.
3. Each subtree is itself a binary search tree.
A BST is a binary tree in which the left
subtree contains key values less than
the root and the right subtree contains
key values greater than or equal to the
root.

BINARY SEARCH TREE


Basic operations:
Binary Search Tree (BST) Binary tree (BT)
Basic operations:
Construction build a null tree.
Destroy - delete all items in the tree.
Empty check the tree is empty or not.

Return TRUE if the tree is empty;


Return FALSE if the tree is not empty.
Insert insert a new node into the tree.
Delete delete a node from a tree.
Traversal traverse, access, and process an item in the tree.
Search search an item in the tree.

BUILDING A BINARY
SEARCH TREE
Example

Process to create a
tree;
12 22 8 19 10
9 20 4 2 6

DELETE A NODE FROM BINARY


SEARCH TREE
To delete a node from a binary search tree, we must first

locate it.
There are four possible cases when we delete a node.
G

DELETE A NODE FROM BINARY


SEARCH TREE
Case 1:
The node to be deleted has no children leave node
(e.g. A, C, F, I, L).
All we need to do is set the delete nodes parent to null
(e.g. B, E, J, M) and the leave node will be deleted.

Case 2:
The node to be deleted has only a right subtree (e.g. E or
H).
If there is only a right subtree, then we can simply attach
the right subtree to the delete nodes parent.

DELETE A NODE FROM BINARY


SEARCH TREE
Case 3 :
The node to be deleted has only a left subtree (e.g. J and M).
If there is only a left subtree, then we attach the left subtree to the delete
nodes parent.

Case 4 :

The node to be deleted has two subtrees (e.g. B, D, G and K).


We try to maintain the existing structure as much as possible by finding data to
take the deleted datas place. This can be done in one or two ways:
1.
find the largest node in the deleted nodes left subtree and move its data
to replace the deleted nodes data, or
2.
find the smallest node on the deleted nodes right subtree and move its
data to replace the deleted nodes data.

Predecessor is the rightmost node in the left subtree for the deleted
node. (e.g. A if B is deleted, C if D is deleted, F if G is deleted and J if K
is deleted).

DELETE A NODE FROM BINARY


SEARCH TREE
Example

BST CLASS IMPLEMENTATION


class BSTNode
{
Object root;
BSTNode left, right;

BSTNode (Object root) {


this.root=root;
}
bool searchBST(BSTNode t, int n);
void AddNode(BSTNode t, int newItem);
bool isBSTEmpty (BSTNode t);
void InsertNodeBST(BSTNode t, int newItem);
void FindDescendant(BSTNode t, BSTNode q);
void DeleteNodeBST(BSTNode t, int n);
void destroyBST(BSTNode t);
void preOrderBST(BSTNode t);
void inOrderBST(BSTNode t);
void postOrderBST(BSTNode t);
};

The implementation file begins as follows:


The implementations of the classs member
functions are included at this point in the
implementation file.

Create BST
Create BST, initialize the root (i.e. root) to

NULL, this indicates as the new empty BST.


Function definition:
public BSTNode (Object root)
{
root = NULL; // no memory allocated until
node inserted into BST
} // end constructor

Destroy BST
Destroy BST deletes all data in a BST and

recycle their memory.


Function definition:
void destroyTree (BSTNode pWalk) {
if (pWalk != NULL) {
destroyTree(pWalk.leftP);
destroyTree(pWalk.rightP);
delete pWalk;
pWalk = NULL;
} // end if
} // end destroyTree

Empty BST
Empty BST is a module that returns a Boolean

indicating if there is data in the BST or if it is


empty. Thus, it returns TRUE if the BST is
empty & FALSE if there is data.
Function definition:
bool isEmpty () {
return root == NULL;
} // end isEmpty

Insert
Inserting a new node into a BST need to follow

the left or right branch to down the tree until


null subtree is found.

Insert -Function definition


void insertBST (int newItem) {
bool found = false;
pWalk = root;
parent = NULL;

for (;;){
if (found == true || pWalk ==NULL) break;
parent = pWalk;
if (newItem < pWalk.data)
pWalk = pWalk.leftP;
else if (newItem > pWalk.data)
pWalk = pWalk.rightP;
else if
found = true;
} // end for

if (found == true)
System.out.println(Item already in the tree);
else {
pWalk = treeNode(newItem);
if (parent = NULL)
root = pWalk;
else if (newItem < parent.data)
parent.leftP = pWalk;
else
parent.rightP = pWalk;
} // end else
} // end insertBST

// create new tree node


BSTNode treeNode(int newItem) {
pNew = new BSTNode;
pNew.data = newItem;
pNew.leftP = NULL;
pNew.rightP = NULL;
return pNew;
} // end treeNode

Delete
To delete a node from a BST, first the element

must be found.
Four possible cases need to be considered
when deleting a node. (section 7.5.2)

Delete -Function definition


void deleteBST(int delItem) {
bool found;
BSTNode x, parent;
search2(delItem, found, x, parent);
if (found == false){
System.out.println(Item not in the BST );
return; // break;
}
// else node has 2 children
if (x.leftP != NULL && x.rightP != NULL) {
BSTNode xSucc = x.rightP;
parent = x;
while (xSucc.leftP != NULL) {
parent = xSucc;
xSucc = xSucc.leftP;
}
x.data = xSucc.data;
x = xSucc;
} // end if
// proceed with case where node has 0 or 1 child
BSTNode subtree = x.leftP;
if (subtree == NULL)
subtree = x.rightP;
if (parent == NULL)
// root being deleted
root = subtree;
else if (parent.leftP == x)
// left child or parent
parent.leftP = subtree;
else

void search2 (int delItem, bool found, BSTNode


locptr, BSTNode parent) {
locptr = root;
parent = NULL;
found = false;
for (;;) {
if (found == true || locptr == NULL) return;
if (delItem < locptr.data) {
parent = locptr;
locptr = locptr.leftP;
} // end if
else if (delItem > locptr.data) {
parent = locptr;
locptr = locptr.rightP;
} // end else if
else

parent.rightP = subtree; // right child or parent

found = true

delete x;

} // end for

} // end deleteBST

} // end search2

Traverse
Traversing algorithm is moving through the

BST and visiting each node exactly once.

There are three ways of doing a traversal

namely preorder traversal, inorder traversal


and postorder traversal (section 7.4).

Travers Function Definition


void preorderTraverse () // preOrder traversal
{
preOrder(root);
} // end preorderTraverse
void preOrder (BSTNode pWalk); {
if (pWalk != NULL)
{
System.out.print( pWalk.data + );
preOrder (pWalk.leftP);
preOrder (pWalk.rightP);
}
} // end preOrder
void inorderTraverse ( ) { // inOrder traversal
inOrder (root);
} // end inorderTraverse
void inOrder (BSTNode pWalk); {
if (pWalk != NULL)
{
inOrder (pWalk.leftP);
System.out.print( pWalk.data + );
inOrder (pWalk.rightP);
}
} // end inOrder

void postOrderTraverse ( )
{
// postOrder
traversal
postOrder (root);
} //end preorderTraverse
void postOrder (BSTNode pWalk); {
if (pWalk != NULL)
{
postOrder (pWalk.leftP);
postOrder (pWalk.rightP);
System.out.print( pWalk.data + );
}
} // end preorder

Search
Search algorithm is used to find a specific

node in the tree.


If the target value, newItem is greater than
the root tree then the search is carry on the
right subtrees, whereas if the target value,
newItem is smaller than the root tree, search
is carry on the left subtrees.

Search- Function Definition


bool searchBST (int targetItem);
{
bool found = false;
pWalk = root;
for (;;)
{
if (found == true || pWalk == NULL) break;
if (targetItem < pWalk.data)
pWalk = pWalk.leftP;

// traverse to left subtree

else if (targetItem > pWalk.data)


pWalk = pWalk.rightP;

// traverse to right subtree

else
found = true;

// targetItem found

} // end for
return found;
} //end searchBST

Exercises ??
1. Create a binary search tree using the following data entered as a
sequential set:
14, 23, 7, 10, 33, 56, 80, 66, 70
2. Insert 44 and 50 into the tree created in Q1.
3. Delete the node containing 60 from the BST in the figure below.
4.Delete the node containing 85 from the BST in below figure.
70

60

50

45

80

65

55

75

85

90

AVL TREES
AVL tree has been created by two Russian mathematicians;

G.M. Adelson-Velskii and E.M.Landis in 1962.


Known as Height-Balanced Binary Search Tree.
AVL tree is a search tree in which the heights of the subtrees
differ by no more than one.

The heights of left subtrees and right subtrees are

represented by HL and HR respectively.


AVL Subtrees Height = | HL HR | 1,
Therefore, AVL balance factor for each
node is either 0, 1, @ -

BALANCING TREES
Rotation is a transformation process to convert unbalanced

binary search tree to AVL tree.


Unbalanced BST falls into one of this four cases:
Left of left
A subtree of a tree that is left high has also become left
high.
Right of right
A subtree of a tree that is right high has also become right high.
Right of left
A subtree of a tree that is left high has become right high.
Left of right
A subtree of a tree that is right high has become left high.

BALANCING TREESContinued

These four cases are seen in figure 13 below:

BALANCING TREESContinued

BALANCING TREESContinued

Rotation transformation is implemented to

overcome the four cases unbalanced trees:


Left of left one way rotation to the right
(single right) for out of balance node.

BALANCING TREESContinued

Right of right one way rotation to the left

(single left) for out of balance node.


Note: The first two
cases required single
rotations to balance
the trees. We now
study two out of
balance conditions in
which we need to
rotate two nodes, one
to the left and one to
the right, to balance

BALANCING TREESContinued

Right of left one way rotation of left subtree

to the left, followed by one way rotation of


root (subtree) to the right (double right).

BALANCING TREESContinued

Left of right one way rotation of right

subtree to the right, followed by one way


rotation of root (subtree) to the left (double
left).

BUILDING AVL TREES USING INSERTION


AND ROTATION
Example
Process to build
an AVL tree
10 9 5 12 3 7 25
40

MULTIWAY TREES: PEPOHON-B (B-TREE)


An m-way tree is a search tree in which each node can have

from 0 to m subtrees, where m is defined as the order of the


tree.

In 1970, two computer scientists working for Boeing Company

have created a new tree structure called the B-tree.

MULTIWAY TREES: PEPOHON-B


(B-TREE)
B-Tree is an m-way search tree with the

following properties:
The root is either a leaf or it has 2 m

subtrees.
All internal nodes have at least [m/2] non-null
subtrees and at most m non-null subtrees.
All leaf nodes are at the same level; that is, the
tree is perfectly balanced.
A leaf node has at least [m/2] 1 and at most m
1 entry.

MULTIWAY TREES: PEPOHON-B


(B-TREE)
From the definition of a B-tree, it should be

apparent that a B-tree is a perfectly balanced


m-way tree in which each node with the
possible exception of the root is at least half
full.

BUILDING A B-TREE (OF


ORDER 5)

DELETING A NODE IN BTREE

Exercises ??
1. Draw the B-tree of order 3 created by inserting the following data arriving in
sequence:
92 24 6 7 11 8 22 4 5 16 19 20 78
2. Draw the B-tree of order 4 created by inserting the following data arriving in
sequence:
92 24 6 7 11 8 22 4 5 16 19 20 78
3. Using the B-tree of order 3 shown in below figure, delete 63,
90, 41, and 60, in each step, show the resulting B-tree.

You might also like