Topic 8 Binary Tree
Topic 8 Binary Tree
DATA STRUCTURES
Zulaile Mabni
TOPIC 8
BINARY TREE
1
CHAPTER OBJECTIVES
To learn how to use a tree to represent a
hierarchical organization of information.
Malik D.S.
To learn how to use recursion to process
trees.
To understand the different ways of
traversing a tree.
To understand the difference between binary
Malik D.S.
The node at the top of a tree is called its
root.
The links from a node to its successors are
called branches.
The successors of a node are called its
children.
The predecessor of a node is called its
parent. 3
TREE TERMINOLOGY (CONTINUED)
Each node in a tree has exactly one parent
except for the root node, which has no
parent.
Malik D.S.
Nodes that have the same parent are
siblings.
A node that has no children is called a leaf
node.
A generalization of the parent-child
Malik D.S.
such that:
T has a special node called the root node;
T has two sets of nodes, LT and RT, called the
left subtree and right subtree of T, respectively;
LT and RT are binary trees
5
BINARY TREE
Malik D.S.
6
BINARY TREE WITH ONE NODE
Malik D.S.
The root node of the binary tree = A
LA = empty
RA = empty
7
BINARY TREE WITH TWO NODES
Malik D.S.
8
BINARY TREE WITH TWO NODES
Malik D.S.
9
VARIOUS BINARY TREES WITH THREE
NODES
Malik D.S.
10
BINARY TREES
Following class defines the node of a binary tree:
Malik D.S.
{
Object info;
BinaryTreeNode llink;
BinaryTreeNode rlink;
}
11
NODES
For each node:
Data is stored in info
Malik D.S.
The reference to the left child is stored in llink
The reference to the right child is stored in rlink
12
GENERAL BINARY TREE
Malik D.S.
13
BINARY TREE DEFINITIONS
Leaf: node that has no left and right
children.
Malik D.S.
Parent: node with at least one child node.
Malik D.S.
If the binary tree is empty, height is 0
If the binary tree is nonempty:
and add 1
if(p is NULL)
height(p) = 0
15
else
height(p) = 1 + max(height(p.llink),height(p.rlink))
HEIGHT OF A BINARY TREE
Method to implement above algorithm:
Malik D.S.
private int height(BinaryTreeNode p)
{
if(p == NULL)
return 0;
else
return 1 + max(height(p.llink),
height(p.rlink));
}
16
HEIGHT OF BINARY TREE
Malik D.S.
17
SOME TYPES OF BINARY TREES
Expression tree
Each node contains an operator or an operand
Malik D.S.
Huffman tree
Represents Huffman codes for characters that
might appear in a text file
Huffman code uses different numbers of bits to
encode letters as opposed to ASCII or Unicode
Binary search trees
Allelements in the left subtree precede those in
the right subtree
18
SOME TYPES OF BINARY TREES
(CONTINUED)
Malik D.S.
19
FULL AND COMPLETE BINARY TREE
A full binary tree (sometimes proper
binary tree or 2-tree) is a tree in which
every node has zero or two children except
Malik D.S.
for the leaves.
A perfect binary tree (sometimes
20
BINARY TREE TRAVERSAL
Must start with the root, then
Visit the node first
Malik D.S.
or
Visit the subtrees first
Three different traversals
Inorder
Preorder
Postorder
21
TRAVERSALS
Inorder
Traverse the left subtree
Visit the node
Malik D.S.
Traverse the right subtree
Preorder
Visitthe node
Traverse the left subtree
Traverse the right subtree
22
TRAVERSALS
Postorder
Traverse the left subtree
Malik D.S.
Traverse the right subtree
Visit the node
23
BINARY TREE: TRAVERSAL
Malik D.S.
Inorder :BDAC
Preorder : A B D C
24
Postorder: D B C A
BINARY TREE: INORDER TRAVERSAL
Malik D.S.
{
if(p != NULL)
{
inorder(p.llink);
System.out.println(p.info + “ “);
inorder(p.rlink);
}
}
25
BINARY TREE: PREORDER TRAVERSAL
Malik D.S.
{
if(p != NULL)
{
System.out.println(p.info + “ “);
preorder(p.llink);
preorder(p.rlink);
}
}
26
BINARY TREE: POSTORDER TRAVERSAL
Malik D.S.
{
if(p != NULL)
{
postorder(p.llink);
postorder(p.rlink);
System.out.println(p.info +
“ “);
}
}
27
IMPLEMENTING BINARY TREES:
CLASS BINARYTREE METHODS
isEmpty
inorderTraversal • Copy
Malik D.S.
preorderTraversal • Inorder
postorderTraversal • Preorder
treeHeight
treeNodeCount
• postorder
treeLeavesCount
• Height
destroyTree • Max
copyTree • nodeCount
• leavesCount
28
BINARY SEARCH TREES
Data in each node
Largerthan the data in its right child
Malik D.S.
Smaller than the data in its left child
A binary search tree, t, is either empty or:
T has a special node called the root node
T has two sets of nodes, LT and RT, called the
left subtree and right subtree of T, respectively
Key in root node larger than every key in left
subtree and smaller than every key in right
subtree
LT and RT are binary search trees
29
BINARY SEARCH TREES
Malik D.S.
30
OPERATIONS PERFORMED ON BINARY
SEARCH TREES
Malik D.S.
empty
Search the binary search tree for a particular
item
Insert an item in the binary search tree
31
OPERATIONS PERFORMED ON BINARY
SEARCH TREES
Malik D.S.
Find the number of nodes in the binary search
tree
Find the number of leaves in the binary search
tree
Traverse the binary search tree
32
Exercises:
50
30 90
Malik D.S.
12 40 86 100
Malik D.S.
access the root element of t;
inOrder(rightTree(t));
} // if
} // inOrder traversal
34
Left – Root – Right
50
30 90
Malik D.S.
12 40 86 100
Malik D.S.
postOrder(leftTree(t));
postOrder(rightTree(t));
access the root element of t;
} // if
} // postOrder traversal
Answer: 12 40 30 86 100 90 50
37
3. preOrder (t)
{
if (t is not empty)
{
Malik D.S.
access the root element of t;
preOrder (leftTree (t));
preOrder (rightTree (t));
} // if
} // preOrder traversal
Answer: 50 30 12 40 90 86 100
39
TRAVERSALS OF EXPRESSION TREES
Malik D.S.
parenthesis where they belong (infix form)
A postorder traversal of an expression tree results
in postfix form
A preorder traversal of an expression tree results
in prefix form
40
TRAVERSALS OF EXPRESSION TREES
An inorder traversal of an expression tree
inserts parenthesis where they belong.
Malik D.S.
An infix expression is obtained.
41
Determine the order in which the elements
would be accessed during a post-order
traversal. Hint: An operator immediately
follows its operands.
Malik D.S.
+
– /
X Y Z *
42
A B
Answer: X, Y, -, Z, A, B, *, / +
Malik D.S.
Postfix!
43
Determine the order in which the elements
would be accessed during a pre-order
traversal. Hint: An operator immediately
precedes its operands.
Malik D.S.
+
– /
X Y Z *
44
A B
Answer: +, -, X, Y, /, Z, *, A, B
Malik D.S.
Prefix!
45
SEARCHING A BINARY TREE
Malik D.S.
46
INSERTION INTO A BINARY SEARCH
TREE
Malik D.S.
47
add (73);
80
40 90
Malik D.S.
60
48
50 75
After inserting (73)
80
40 90
Malik D.S.
60
50 75
73
49
Given the following data:
HP, Compaq, Apple, Sony, Samsung, Compact, Toshiba,
Acer, Dell
Draw the Binary Search Tree
Malik D.S.
50
BINARY SEARCH TREE DELETION
After deleting the desired item, the resulting tree must be a
binary search tree.
Four cases (Refer to binary search tree on the next slide):
1) The node to be deleted has no left and right subtrees.
Malik D.S.
Example: Node with data 46
Malik D.S.
52
REMOVING FROM A BINARY SEARCH
TREE
Malik D.S.
53
Exercises:
remove (50);
80
40 90
Malik D.S.
60
50 75
54
73
After removing 50:
remove (40);
80
40 90
Malik D.S.
60
75
55
73
After removing 40:
80
Malik D.S.
60 90
75
73 56
remove (80);
80
60 110
Malik D.S.
75 100 150
73 85 105
57
95
The element 80 has two children, so we cannot simply
Malik D.S.
unlink 80 from the tree: that would create a hole.
58
We can replace 80 with either its predecessor, 75, or
its successor, 85.
Malik D.S.
The successor of an element is the leftmost element in the
right subtree.
The predecessor of an element is the rightmost element in
the left subtree.
We’ll choose its successor.
Replace 80 with 85, and then remove 85.
59
After removing 80:
85
Malik D.S.
60 110
75 100 150
73 95 105
60
CLASS TREESET AND INTERFACE
SEARCH TREE
Malik D.S.
61
BINARYSEARCHTREE CLASS
Malik D.S.
62
AVL (HEIGHT-BALANCED TREES)
A perfectly balanced binary tree is a binary
tree such that:
Malik D.S.
The height of the left and right subtrees of the
root are equal
The left and right subtrees of the root are
perfectly balanced binary trees
63
PERFECTLY BALANCED BINARY TREE
Malik D.S.
64
AVL (HEIGHT-BALANCED TREES)
An AVL tree (or height-balanced tree) is a
binary search tree such that:
Malik D.S.
The height of the left and right subtrees of the
root differ by at most 1
The left and right subtrees of the root are AVL
trees
65
AVL TREES
Malik D.S.
66
NON-AVL TREES
Malik D.S.
67
BALANCE FACTOR
The balance factor of x, written bf(x), is
defined as bf(x) = xr – xl.
Malik D.S.
Let x be a node in the AVL tree T. Then:
1. If x is left high, then bf(x) = -1
2. If x is equal high, then bf(x) = 0
3. If x is right high, then bf(x) = 1
We say that the node x violates the balance
criteria if | xr – xl| > 1, that is, the height of
the left and right subtrees of x differ by more
than 1.
68
INSERTION INTO AVL TREE
Malik D.S.
69
INSERTION INTO AVL TREES
Malik D.S.
70
INSERTION INTO AVL TREES
Malik D.S.
71
INSERTION INTO AVL TREES
Malik D.S.
72
INSERTION INTO AVL TREES
Malik D.S.
73
AVL TREE ROTATIONS
Reconstruction procedure: rotating tree
left rotation and right rotation
Malik D.S.
Suppose that the rotation occurs at node x
Left rotation: certain nodes from the right
subtree of x move to its left subtree; the root
of the right subtree of x becomes the new
root of the reconstructed subtree
Right rotation at x: certain nodes from the
left subtree of x move to its right subtree; the
root of the left subtree of x becomes the new 74
root of the reconstructed subtree
AVL TREE ROTATIONS
Malik D.S.
75
AVL TREE ROTATIONS
Malik D.S.
76
AVL TREE ROTATIONS
Malik D.S.
77
AVL TREE ROTATIONS
Malik D.S.
78
AVL TREE ROTATIONS
Malik D.S.
79
AVL TREE ROTATIONS
Malik D.S.
80
DELETION FROM AVL TREES
Case 1: the node to be deleted is a leaf
Case 2: the node to be deleted has no right
Malik D.S.
child, that is, its right subtree is empty
Case 3: the node to be deleted has no left
81
CHAPTER REVIEW
A tree is a recursive, nonlinear data structure that is
used to represent data that is organized as a
hierarchy.
Malik D.S.
A binary tree is a collection of nodes with three
components: a reference to a data object, a reference
to a left subtree, and a reference to a right subtree.
In a binary tree used for arithmetic expressions, the
root node should store the operator that is evaluated
last.
A binary search tree is a tree in which the data stored
in the left subtree of every node is less than the data
stored in the root node, and the data stored in the
right subtree is greater than the data stored in the
root node. 82
REFERENCES
Malik D.S.
Koffman E., Wolfgang P., Objects, Abstraction, Data
Structures And Design Using Java, John Wiley & Sons,
2005.
Malik D.S, Nair P.S., Data Structures Using Java,
Course Technology, 2003.
83