Topic 5 Binary Tree - Updated
Topic 5 Binary Tree - Updated
BINARY TREE
Zulaile Mabni
To learn how to use a tree to represent a hierarchical
organization of information.
To learn how to use recursion to process trees.
To understand the different ways of traversing a tree.
To understand the difference between binary trees, and binary
search trees.
To learn how to implement binary trees, and binary search
trees using linked data structures.
Learn about AVL (height-balanced) trees
2
A tree consists of a collection of elements or nodes, with each
node linked to its successors.
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
Each node in a tree has exactly one parent except for the root
node, which has no parent.
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 relationship is the
ancestor-descendent relationship.
4
A binary tree is a hierarchical structure.
Definition: A binary tree, T, is either empty or 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
6
The root node of the binary tree = A
LA = empty
RA = empty
7
8
9
10
Following class defines the node of a binary tree:
11
For each node:
Data is stored in info
The reference to the left child is stored in llink
The reference to the right child is stored in rlink
12
13
Leaf: node that has no left and right children.
Parent: node with at least one child node.
Level of a node: number of branches on the path from root to
node.
The level of the root node is 0
The level of the children of the root node is 1
Height of a binary tree: The length of the path from the root to
the deepest node in the tree. A (rooted) tree with only a node
(the root) has a height of zero.
14
Recursive algorithm to find height of binary
tree: (height(p) denotes height of binary tree
with root p):
If the binary tree is empty, height is 0
If the binary tree is nonempty:
Find the height of left subtree & right subtree
Find the maximum of these two heights and add 1
if(p is NULL)
height(p) = 0
else
height(p) = 1 + max(height(p.llink),height(p.rlink))
15
Method to implement above algorithm:
16
Height = 3
17
Expression tree
Each node contains an operator or an operand
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
All elements in the left subtree precede those in the right subtree
18
19
A full binary tree (sometimes proper binary tree or 2-tree) is
a tree in which every node has zero or two children except for
the leaves.
A perfect binary tree (sometimes complete binary tree) is a
full binary tree in which all leaves are at the same depth.
20
Must start with the root, then
Visit the node first
or
Visit the subtrees first
21
Inorder
Traverse the left subtree
Visit the node
Traverse the right subtree
Preorder
Visit the node
Traverse the left subtree
Traverse the right subtree
22
Postorder
Traverse the left subtree
Traverse the right subtree
Visit the node
23
Inorder :BDAC
Preorder : A B D C
Postorder: D B C A
24
private void inorder(BinaryTreeNode p)
{
if(p != NULL)
{
inorder(p.llink);
System.out.println(p.info + “ “);
inorder(p.rlink);
}
}
25
private void preorder(BinaryTreeNode p)
{
if(p != NULL)
{
System.out.println(p.info + “ “);
preorder(p.llink);
preorder(p.rlink);
}
}
26
private void postorder(BinaryTreeNode p)
{
if(p != NULL)
{
postorder(p.llink);
postorder(p.rlink);
System.out.println(p.info + “ “);
}
}
27
Data in each node
Larger than the data in its left child
Smaller than the data in its right child
28
29
Determine whether the BST is empty
Insert an item in the BST
Find the height of the BST
Find the number of nodes in the BST
Find the number of leaves in the BST
Traverse the BST (preorder, inorder, postorder)
Find the minimum/maximum value in the BST
Find the total/average value in the BST
30
Exercises on BST Traversal
50
30 90
12 40 86 100
31
1. inOrder(t)
{
if (t is not empty)
{
inOrder(leftTree(t));
access the root element of t;
inOrder(rightTree(t));
} // if
} // inOrder traversal
30 90
12 40 86 100
Answer: 12 40 30 86 100 90 50
35
3. preOrder (t)
{
if (t is not empty)
{
access the root element of t;
preOrder (leftTree (t));
preOrder (rightTree (t));
} // if
} // preOrder traversal
Answer: 50 30 12 40 90 86 100
37
An inorder traversal of an expression tree inserts 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
38
An inorder traversal of an expression tree inserts parenthesis
where they belong.
An infix expression is obtained.
39
Determine the order in which the elements
would be accessed during a post-order
traversal. Hint: An operator immediately
follows its operands.
+
– /
X Y Z *
A B 40
Answer: X, Y, -, Z, A, B, *, / +
Postfix!
41
Determine the order in which the elements
would be accessed during a pre-order
traversal. Hint: An operator immediately
precedes its operands.
+
– /
X Y Z *
A B 42
Answer: +, -, X, Y, /, Z, *, A, B
Prefix!
43
44
add (73);
80
40 90
60
50 75 45
After inserting (73)
80
40 90
60
50 75
73
46
Given the following data:
HP, Compaq, Apple, Sony, Samsung, Compact, Toshiba, Acer, Dell
Draw the Binary Search Tree
47
A perfectly balanced binary tree is a binary tree such that:
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
48
49
An AVL tree (or height-balanced tree) is a binary search tree
such that:
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
50
51
52
The balance factor of x, written bf(x), is defined as bf(x) = xr –
xl.
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
53
54
55
56
57
58
Reconstruction procedure: rotating tree
left rotation and right rotation
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 root of the reconstructed subtree
59
60
61
62
63
64
65
66
67
A tree is a recursive, nonlinear data structure that is used to
represent data that is organized as a hierarchy.
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.
68
Collin, Williams, Data Structures and The Java Collections
Framework, 2nd Edition, McGraw Hill , 2005.
69