Topic 7 Binary Tree
Topic 7 Binary Tree
Zulaile Mabni
CHAPTER OBJECTIVES
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 and arrays.
Malik D.S.
TREE TERMINOLOGY
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.
Malik D.S.
Malik D.S.
BINARY TREES
A binary tree is a hierarchical structure. Definition: A binary tree, T, is either empty or such that:
Malik D.S.
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
BINARY TREE
Malik D.S.
Malik D.S.
Malik D.S.
Malik D.S.
10
Malik D.S.
BINARY TREES
Following class defines the node of a binary tree:
protected class BinaryTreeNode { Object info; BinaryTreeNode llink; BinaryTreeNode rlink; }
Malik D.S.
11
NODES
Malik D.S.
12
13
Malik D.S.
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
Malik D.S.
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
Malik D.S.
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
private int height(BinaryTreeNode p) { if(p == NULL) return 0; else return 1 + max(height(p.llink), height(p.rlink)); }
16
Malik D.S.
Height = 3
17
Expression tree
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
All elements in the left subtree precede those in the right subtree
18
19
Malik D.S.
20
Malik D.S.
or
21
TRAVERSALS
Inorder
Traverse the left subtree Visit the node Traverse the right subtree
Malik D.S.
Preorder
Visit the node Traverse the left subtree Traverse the right subtree
22
TRAVERSALS
Postorder
Traverse the left subtree Traverse the right subtree Visit the node
Malik D.S.
23
Malik D.S.
Inorder
:BDAC
24
Preorder : A B D C Postorder: D B C A
25
Malik D.S.
26
Malik D.S.
27
Malik D.S.
28
Malik D.S.
Malik D.S.
29
30
Malik D.S.
31
Malik D.S.
32
Malik D.S.
Exercises:
50
30
90
Malik D.S.
12
40
86
100
Determine the order in which the elements would be accessed during an in-order traversal.
33
1. inOrder(t) { if (t is not empty) { inOrder(leftTree(t)); access the root element of t; inOrder(rightTree(t)); } // if } // inOrder traversal Left Root Right
34
Malik D.S.
50
30 12 90
Malik D.S.
40 86
100
2.
postOrder (t) { if (t is not empty) { postOrder(leftTree(t)); postOrder(rightTree(t)); access the root element of t; } // if } // postOrder traversal Left Right Root
36
Malik D.S.
Malik D.S.
3.
preOrder (t) { if (t is not empty) { access the root element of t; preOrder (leftTree (t)); preOrder (rightTree (t)); } // if } // preOrder traversal
Malik D.S.
38
Malik D.S.
40
Malik D.S.
41
Malik D.S.
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.
*
42
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.
*
44
Malik D.S.
Answer: +, -, X, Y, /, Z, *, A, B
Prefix!
45
46
Malik D.S.
47
Malik D.S.
add (73); 80
40
90
Malik D.S.
60
48
50
75
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
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. Example: Node with data 46
Malik D.S.
2) The node to deleted has no left subtree but has a nonempty right subtree. Example: Node with data 30
3) The node to be deleted has no right subtree but has a nonempty left subtree Example: Node with data 80 4) The node to be deleted has nonempty left and right subtrees. Example: Node with data 50
51
52
Malik D.S.
53
Malik D.S.
Exercises:
remove (50);
80 40 90
Malik D.S.
60
50
75
54
73
remove (40); 80 40 90
Malik D.S.
60
75
55
73
Malik D.S.
80 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 unlink 80 from the tree: that would create a hole.
Of the elements already in the tree, two could replace 80 (and then have the original deleted) without destroying the binary search tree properties. Which two?
58
Malik D.S.
We can replace 80 with either its predecessor, 75, or its successor, 85. 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. Well choose its successor. Replace 80 with 85, and then remove 85.
59
Malik D.S.
85 60 110
Malik D.S.
75
100
150
73
95
105
60
61
Malik D.S.
BINARYSEARCHTREE CLASS
62
Malik D.S.
CHAPTER REVIEW
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.
63
Malik D.S.
REFERENCES
Collin, Williams, Data Structures and The Java Collections Framework, 2nd Edition, McGraw Hill , 2005. 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.
Malik D.S.
64