Chap 7 Trees
Chap 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
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).
BINARY TREES
Binary tree is a tree in which no node can have more
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,
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
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:
*
EXPRESSION TREE
EXAMPLE
X * (Y / -Z)
EXPRESSION TREE
BUILD AN EXPRESSION - EXAMPLE
(A - B + C) * (-D)
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
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
*
/
-
+
f
h
g
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.
BUILDING A BINARY
SEARCH TREE
Example
Process to create a
tree;
12 22 8 19 10
9 20 4 2 6
locate it.
There are four possible cases when we delete a node.
G
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.
Case 4 :
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).
Create BST
Create BST, initialize the root (i.e. root) to
Destroy BST
Destroy BST deletes all data in a BST and
Empty BST
Empty BST is a module that returns a Boolean
Insert
Inserting a new node into a BST need to follow
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
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)
found = true
delete x;
} // end for
} // end deleteBST
} // end search2
Traverse
Traversing algorithm is moving through the
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
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;
BALANCING TREES
Rotation is a transformation process to convert unbalanced
BALANCING TREESContinued
BALANCING TREESContinued
BALANCING TREESContinued
BALANCING TREESContinued
BALANCING TREESContinued
BALANCING TREESContinued
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.
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.