Binary Tree

advertisement
TCS1011 Data Structures & Algorithms
Binary Trees
Binary Trees
Binary Trees
A binary tree is a tree, where every node has at most two children, one drawn to the left,
called the left child, and one to the right, the right child. Every node in a binary tree is
the root of at most two sub-trees, namely, its left sub-tree, and the right sub-tree.
Level 0
A Root
Level 1
B
C
Left child of A
Right child of A
Level 2
D Leaf
Level 3
F
E
H
I
G
J
Left sub-tree of C
K
Level 4
Height of the tree h =5.
The data type for a node in a binary tree can be defined as follows:
public class Node{
int data;
Node left;
Node right;
}
//Data field
//A reference to the node’s left child.
//A reference to the node’s right child.
If a binary tree contains n nodes, then its height is at least log2 (n+1), and at most n.
Traversal of Binary Trees
1
TCS1011 Data Structures & Algorithms
Binary Trees
Traversal of a binary tree refers to the process of visiting all the nodes of the tree, in some
specific sequence. For a linked list, the traversal was done in the natural order of the list,
starting from the first node and ending at the last. In a tree, there are many ways in which
one could traverse all the nodes.
Visiting a node generally means retrieving the data item contained in the node, and
sending it to some process (printing, for example).
The three traversal orders are defined as follows:
(i) Preorder Traversal: (VLR)
Visit the current node.
Traverse the left sub-tree of the current node.
Traverse the right sub-tree of the current node.
Preorder traversal can be implemented as a recursive function:
The tree given on the previous page is visited in the following order using preorder
traversal:
A, B, D, E, H, C, F, I, K, J, G
(ii) Inorder Traversal: (LVR)
Traverse the left sub-tree of the current node.
Visit the current node.
Traverse the right sub-tree of the current node.
Inorder traversal can be implemented as a recursive function:
The tree given is visited in the following order using inorder traversal:
D, B, H, E, A, I, K, F, J, C, G
(iii) Postorder Traversal: (LRV)
Traverse the left sub-tree of the current node.
2
TCS1011 Data Structures & Algorithms
Binary Trees
Traverse the right sub-tree of the current node.
Visit the current node.
Postorder traversal can be implemented as a recursive function:
The tree given is visited in the following order using postorder traversal:
D, H, E, B, K, I, J, F, G, C, A
Representation of Algebraic Expressions
A binary tree can be used to represent an algebraic expression that involves only binary
arithmetic operators +, -, /, and *. The root node holds an operator, and each of its subtrees represents either a variable (operand), or another expression. The following tree
represents the expression A*(B+C)
*
A
+
B
C
Traversing the above tree inorder will generate the sequence A*B+C, and here we need to
insert the parenthesis to get the correct expression.
Traversing the tree in preorder will generate the expression *A+BC, which is called the
prefix notation.
Traversing the tree in postorder will generate the expression ABC+*, which is called the
postfix notation.
Binary Search Tree
A binary search tree is a data structure whose data elements are organized as a binary tree,
with the additional condition that if a node contains a value k, then every node in its left
3
TCS1011 Data Structures & Algorithms
Binary Trees
sub-tree contains a value less than k, and every node in its right sub-tree contains a value
greater than or equal to k. The above condition implies that every left child must have a
key less than its parent, and every right child must have a key greater than or equal to its
parent.
An important consequence of the above property is that an inorder traversal on a binary
tree will always visit the vertices of the tree in ascending order.
65
27
80
15
51
40
30
70
60
92
85
42
35
The inorder traversal of the above tree gives the sequence:
15, 27, 30, 35, 40, 42, 51, 60, 65, 70, 80, 85, 92
With reference to the above sequence, the node containing the value 30 is called the
inorder successor of the node containing the value 27. If a node has a right sub-tree, its
inorder successor is the last left node on this right sub-tree.
The important operations on a binary search tree are:
1. Searching for a value (data item, or key).
2. Inserting a node
3. Deleting a node
4. Finding the minimum or maximum of values stored in the tree.
4
TCS1011 Data Structures & Algorithms
Binary Trees
Searching a binary search tree:
Starting at the root node, the search algorithm compares the search key with the data
stored in the current node.
(a) If the search key is equal to the data of the current node, the value has been
found, and the search is terminated.
(b) If the search key is greater than the data of the current node, the search
proceeds with the right child as the new current node.
(c) If the search key is less than the data of the current node, the search proceeds
with the left child as the new current node.
(d) If the current node is null, the search is terminated as unsuccessful.
Algorithm SearchBST(val root <pointer>,
Val argument <key>)
Search a binary search tree for a given value
Pre : root is the root to a binary tree
Return the node address if the value is found or null if the node is not in the tree
1 if (root is null)
1 return null
2 end if
3 if (argument < root->key)
1 return searchBST(root->left, argument)
4 elseif (argument > root->key)
1 return searchBST(root->right, argument)
5 else
1 return root
6 end if
end searchBST
Since the above search algorithm visits at most one node in each level of the binary tree,
the algorithm runs in (h) time, where h is the height of the tree.
5
TCS1011 Data Structures & Algorithms
Binary Trees
Example :
23
18
44
20
12
35
52
Starting with 23, the binary search examines either 18 or 44, depending on the search key.
From 18 it examines either 12 or 20; from 44 it examines either 35 or 52. Let assumed
that we are searching for 20. Therefore, by comparing 20 is less than the rood value 23,
and because we know that all values less than the root lie in its left subtree, we go left.
We now compare the search argument with the value in the subtree, 18. Because we
know that values greater than the tree root must lie in its right subtree, we go right and
find our desired value.
23
18
20
Inserting a new node in a binary search tree:
The operation of inserting a new node proceeds in the same sequence as the search
operation, until it encounters a “null”, where the new node is inserted and connected to
the previous node as its parent.
6
TCS1011 Data Structures & Algorithms
Binary Trees
65
27
80
15
51
30
Parent 70
60
New 72
7
92
85
TCS1011 Data Structures & Algorithms
Binary Trees
Deleting a node from a binary search tree:
Deleting a node is the most complicated operation required for binary search tree. There
are three cases to consider:
(a) The node to be deleted is a leaf (has no children).
(b) The node to be deleted has one child.
(c) The node to be deleted has two children.
To delete a leaf node, we appropriately change the child field in the node’s parent to point
to null, instead of the node (see figure below).
65
Parent
65
27
27
null
15 < Delete
51
51
To delete a node with one child, we change the appropriate reference in the node’s parent
to point to the child of the deleted node. The child along with its sub-trees, now takes the
place of the deleted node.
30 Parent
25
75
30
< Delete
25
45
38
45
38
68
8
68
TCS1011 Data Structures & Algorithms
Binary Trees
To delete a node with two children, we adopt the following procedure.
Step 1: Replace the node with its inorder successor. Since the node to be deleted has two
children, it has a right sub-tree, and its inorder successor is the last left node in this subtree. This operation, however, involves the deletion of the inorder successor, and this has
to be appropriately taken care of:
Step 2: Since the inorder successor is the last left node in a sub-tree, it cannot have a left
child. Therefore it can have at most one child. If it has a right child, the deletion of the
node causes the right child to occupy its position.
90
90
30 <Delete
25
75
27
Inorder successor
38
25
45
38
75
27
68
40
45
68
40
In the above example, the node marked for deletion has a value 30, and its inorder
successor is the node with value 38 (the last left node in the right sub-tree of the deleted
node). The following operations are performed:
The node containing 30 is replaced with the node containing 38.
The initial position of the node containing 38 is now occupied by its right child (node
containing 40).
9
TCS1011 Data Structures & Algorithms
Binary Trees
Computing the minimum and maximum values in a binary search tree:
To find the minimum in a binary search tree, we go the left child of the root; then go to
the left child of that child and so on, until we get a node that has no left child. In other
words, the minimum value is contained in the last left node in the left sub-tree of the root.
The maximum can be similarly obtained by traversing the right sub-tree, and visiting the
last right child in this tree.
10
Download