Bab 5 - Tree
-
Upload
vijay-fathur -
Category
Documents
-
view
220 -
download
0
Transcript of Bab 5 - Tree
-
7/30/2019 Bab 5 - Tree
1/60
Data Structures Using Java 1
Bab 5
Binary Trees
-
7/30/2019 Bab 5 - Tree
2/60
Data Structures Using Java 2
Chapter Objectives
Learn about binary trees
Explore various binary tree traversal algorithms
Learn how to organize data in a binary search tree
Discover how to insert and delete items in a binary
search tree
Explore nonrecursive binary tree traversalalgorithms
Learn about AVL (height-balanced) trees
-
7/30/2019 Bab 5 - Tree
3/60
Data Structures Using Java 3
Binary Trees
Definition: A binary tree, T, is either empty or
such that:
Thas a special node called the rootnode; Thas two sets of nodes,LTandRT, called the left
subtree and right subtree ofT, respectively;
LTandRTare binary trees
-
7/30/2019 Bab 5 - Tree
4/60
Data Structures Using Java 4
Binary Tree
-
7/30/2019 Bab 5 - Tree
5/60
Data Structures Using Java 5
Binary Tree with One Node
The root node of the binary tree = A
LA = empty
RA = empty
-
7/30/2019 Bab 5 - Tree
6/60
Data Structures Using Java 6
Binary Tree with Two Nodes
-
7/30/2019 Bab 5 - Tree
7/60
Data Structures Using Java 7
Binary Tree with Two Nodes
-
7/30/2019 Bab 5 - Tree
8/60
Data Structures Using Java 8
Various Binary Trees with Three
Nodes
-
7/30/2019 Bab 5 - Tree
9/60
Data Structures Using Java 9
Binary Trees
Following class defines the node of a binary tree:
protected class BinaryTreeNode
{
DataElement info;
BinaryTreeNode llink;
BinaryTreeNode rlink;
}
-
7/30/2019 Bab 5 - Tree
10/60
Data Structures Using Java 10
Nodes
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
-
7/30/2019 Bab 5 - Tree
11/60
Data Structures Using Java 11
General Binary Tree
-
7/30/2019 Bab 5 - Tree
12/60
Data Structures Using Java 12
Binary Tree Definitions
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 pathfrom root to node
Height of a binary tree: number of nodes no the
longest path from root to node
-
7/30/2019 Bab 5 - Tree
13/60
Data Structures Using Java 13
Height of a Binary Tree
Recursive algorithm to find height of binary
tree: (height(p) denotes height of binary tree
with root p):if(p is NULL)
height(p) = 0
else
height(p) = 1 + max(height(p.llink),height(p.rlink))
-
7/30/2019 Bab 5 - Tree
14/60
Data Structures Using Java 14
Height of a Binary Tree
Method to implement above algorithm:
private int height(BinaryTreeNode p){
if(p == NULL)
return 0;
else
return 1 + max(height(p.llink),height(p.rlink));
}
-
7/30/2019 Bab 5 - Tree
15/60
Data Structures Using Java 15
Copy Tree
Useful operation on binary trees is to make
identical copy of binary tree
Method copy useful in implementing copyconstructor and method copyTree
-
7/30/2019 Bab 5 - Tree
16/60
Data Structures Using Java 16
Method copy
BinaryTreeNode copy(BinaryTreeNode otherTreeRoot)
{
BinaryTreeNode temp;
if(otherTreeRoot == null)
temp = null;
else
{
temp = new BinaryTreeNode();
temp.info = otherTreeRoot.info.getCopy();
temp.llink = copy(otherTreeRoot.llink);
temp.rlink = copy(otherTreeRoot.rlink);
}
return temp;
}//end copy
-
7/30/2019 Bab 5 - Tree
17/60
Data Structures Using Java 17
Method copyTree
public void copyTree(BinaryTree otherTree)
{
if(this != otherTree) //avoid self-copy
{root = null;
if(otherTree.root != null) //otherTree is
//nonempty
root = copy(otherTree.root);
}}
-
7/30/2019 Bab 5 - Tree
18/60
Data Structures Using Java 18
Binary Tree Traversal
Must start with the root, then
Visit the node first
or Visit the subtrees first
Three different traversals
Inorder
Preorder
Postorder
-
7/30/2019 Bab 5 - Tree
19/60
Data Structures Using Java 19
Traversals
Inorder
Traverse the left subtree
Visit the node
Traverse the right subtree
Preorder
Visit the node
Traverse the left subtree Traverse the right subtree
-
7/30/2019 Bab 5 - Tree
20/60
Data Structures Using Java 20
Traversals
Postorder
Traverse the left subtree
Traverse the right subtree Visit the node
-
7/30/2019 Bab 5 - Tree
21/60
Data Structures Using Java 21
Binary Tree: Inorder Traversal
-
7/30/2019 Bab 5 - Tree
22/60
Data Structures Using Java 22
Binary Tree: Inorder Traversal
private void inorder(BinaryTreeNode p)
{
if(p != NULL){
inorder(p.llink);
System.out.println(p.info + );
inorder(p.rlink);
}
}
-
7/30/2019 Bab 5 - Tree
23/60
Data Structures Using Java 23
Binary Tree: Preorder Traversal
private void preorder(BinaryTreeNode p)
{
if(p != NULL){
System.out.println(p.info + );
preorder(p.llink);
preorder(p.rlink);
}
}
-
7/30/2019 Bab 5 - Tree
24/60
Data Structures Using Java 24
Binary Tree: Postorder Traversal
private void postorder(BinaryTreeNode p)
{
if(p != NULL){
postorder(p.llink);
postorder(p.rlink);
System.out.println(p.info + );
}
}
-
7/30/2019 Bab 5 - Tree
25/60
Data Structures Using Java 25
Implementing Binary Trees:
class BinaryTree methods isEmpty
inorderTraversal
preorderTraversal postorderTraversal
treeHeight
treeNodeCount
treeLeavesCount destroyTree
copyTree
Copy
Inorder
Preorder postorder
Height
Max
nodeCount leavesCount
-
7/30/2019 Bab 5 - Tree
26/60
Data Structures Using Java 26
Binary Search Trees
Data in each node
Larger than the data in its left child
Smaller than the data in its right child
A binary search tree, t, is either empty or:
Thas a special node called the rootnode
Thas two sets of nodes,LTandRT, called the leftsubtree and right subtree ofT, respectively
Key in root node larger than every key in left subtreeand smaller than every key in right subtree
LTandRTare binary search trees
-
7/30/2019 Bab 5 - Tree
27/60
Data Structures Using Java 27
Binary Search Trees
-
7/30/2019 Bab 5 - Tree
28/60
Data Structures Using Java 28
Operations Performed on Binary
Search Trees Determine whether the binary search tree is empty
Search the binary search tree for a particular item
Insert an item in the binary search tree
Delete an item from the binary search tree
-
7/30/2019 Bab 5 - Tree
29/60
Data Structures Using Java 29
Operations Performed on Binary
Search Trees Find the height of the binary search tree
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
Copy the binary search tree
-
7/30/2019 Bab 5 - Tree
30/60
Data Structures Using Java 30
Binary Search Tree: Analysis
-
7/30/2019 Bab 5 - Tree
31/60
Data Structures Using Java 31
Binary Search Tree: Analysis
Theorem: Let Tbe a binary search tree with n nodes,
where n > 0.The average number of nodes visited in a
search ofTis approximately 1.39log2n
Number of comparisons required to determine whetherx isin Tis one more than the number of comparisons required
to insertx in T
Number of comparisons required to insertx in T same as
the number of comparisons made in unsuccessful search,reflecting thatx is not in T
-
7/30/2019 Bab 5 - Tree
32/60
Data Structures Using Java 32
Binary Search Tree: Analysis
It follows that:
It is also known that:
Solving Equations (10-1) and (10-2)
-
7/30/2019 Bab 5 - Tree
33/60
Data Structures Using Java 33
Nonrecursive Inorder Traversal
-
7/30/2019 Bab 5 - Tree
34/60
Data Structures Using Java 34
Nonrecursive Inorder Traversal:
General Algorithm1. current = root; //start traversing the binary tree at
// the root node
2. while(current is not NULL or stack is nonempty)
if(current is not NULL)
{
push current onto stack;current = current.llink;
}
else
{
pop stack into current;
visit current; //visit the node
current = current.rlink; //move to the right child
}
-
7/30/2019 Bab 5 - Tree
35/60
Data Structures Using Java 35
Nonrecursive Preorder Traversal
General Algorithm1. current = root; //start the traversal at the root node
2. while(current is not NULL or stack is nonempty)
if(current is not NULL)
{
visit current;
push current onto stack;current = current.llink;
}
else
{
pop stack into current;
current = current.rlink; //prepare to visit
//the right subtree
}
-
7/30/2019 Bab 5 - Tree
36/60
Data Structures Using Java 36
Nonrecursive Postorder Traversal
1. current = root; //start traversal at root node
2. v = 0;
3. if(current is NULL)
the binary tree is empty
4. if(current is not NULL)
a. push current into stack;
b. push 1 onto stack;
c. current = current.llink;
d. while(stack is not empty)
if(current is not NULL and v is 0)
{
push current and 1 onto stack;
current = current.llink;
}
-
7/30/2019 Bab 5 - Tree
37/60
Data Structures Using Java 37
Nonrecursive Postorder Traversal
(Continued)else
{
pop stack into current and v;
if(v == 1)
{push current and 2 onto stack;
current = current.rlink;
v = 0;
}
else
visit current;
}
-
7/30/2019 Bab 5 - Tree
38/60
Data Structures Using Java 38
AVL (Height-Balanced Trees)
A perfectly balancedbinary tree is a binary tree
such that:
The height of the left and right subtrees of the root areequal
The left and right subtrees of the root are perfectly
balanced binary trees
-
7/30/2019 Bab 5 - Tree
39/60
Data Structures Using Java 39
Perfectly Balanced Binary Tree
-
7/30/2019 Bab 5 - Tree
40/60
Data Structures Using Java 40
AVL (Height-Balanced Trees)
An AVL tree(or height-balanced tree) is a binary
search tree such that:
The height of the left and right subtrees of the rootdiffer by at most 1
The left and right subtrees of the root are AVL trees
-
7/30/2019 Bab 5 - Tree
41/60
Data Structures Using Java 41
AVL Trees
-
7/30/2019 Bab 5 - Tree
42/60
Data Structures Using Java 42
Non-AVL Trees
-
7/30/2019 Bab 5 - Tree
43/60
Data Structures Using Java 43
Insertion Into AVL Tree
-
7/30/2019 Bab 5 - Tree
44/60
Data Structures Using Java 44
Insertion Into AVL Trees
-
7/30/2019 Bab 5 - Tree
45/60
Data Structures Using Java 45
Insertion Into AVL Trees
-
7/30/2019 Bab 5 - Tree
46/60
Data Structures Using Java 46
Insertion Into AVL Trees
-
7/30/2019 Bab 5 - Tree
47/60
Data Structures Using Java 47
Insertion Into AVL Trees
-
7/30/2019 Bab 5 - Tree
48/60
Data Structures Using Java 48
AVL Tree Rotations
Reconstruction procedure: rotatingtree
left rotationand right rotation
Suppose that the rotation occurs at nodex
Left rotation: certain nodes from the right subtree ofx
move to its left subtree; the root of the right subtree ofx
becomes the new root of the reconstructed subtree
Right rotation atx: certain nodes from the left subtree ofx
move to its right subtree; the root of the left subtree ofx
becomes the new root of the reconstructed subtree
-
7/30/2019 Bab 5 - Tree
49/60
Data Structures Using Java 49
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
50/60
Data Structures Using Java 50
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
51/60
Data Structures Using Java 51
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
52/60
Data Structures Using Java 52
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
53/60
Data Structures Using Java 53
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
54/60
Data Structures Using Java 54
AVL Tree Rotations
-
7/30/2019 Bab 5 - Tree
55/60
Data Structures Using Java 55
Deletion From AVL Trees
Case 1: the node to be deleted is a leaf
Case 2:the node to be deleted has no right child,
that is, its right subtree is empty Case 3:the node to be deleted has no left child,
that is, its left subtree is empty
Case 4:the node to be deleted has a left child and
a right child
-
7/30/2019 Bab 5 - Tree
56/60
Data Structures Using Java 56
Analysis: AVL Trees
Consider all the possible AVL trees of height h. Let Thbe an
AVL tree of height h such that Thhas the fewest number of
nodes. Let Thldenote the left subtree ofT
hand T
hrdenote the
right subtree ofTh. Then:
where | Th| denotes the number of nodes in T
h.
-
7/30/2019 Bab 5 - Tree
57/60
Data Structures Using Java 57
Analysis: AVL Trees
Suppose that Thlis of height h 1 and T
hris of height h 2.
Thlis an AVL tree of height h 1 such that T
hlhas the fewest
number of nodes among all AVL trees of height h 1. Thris
an AVL tree of height h 2 that has the fewest number ofnodes among all AVL trees of height h 2. T
hlis of the form
Th-1 and T
hris of the form T
h-2. Hence:
-
7/30/2019 Bab 5 - Tree
58/60
Data Structures Using Java 58
Analysis: AVL Trees
LetFh+2 = |Th | + 1. Then:
Called a Fibonacci sequence; solution toFh
is given by:
Hence
From this it can be concluded that
-
7/30/2019 Bab 5 - Tree
59/60
Data Structures Using Java 59
Programming Example: Video
Store (Revisited) In Chapter 4,we designed a program to help a video store automate its
video rental process.
That program used an (unordered) linked list to keep track of the videoinventory in the store.
Because the search algorithm on a linked list is sequential and the listis fairly large, the search could be time consuming.
If the binary tree is nicely constructed (that is, it is not linear), then thesearch algorithm can be improved considerably.
In general, item insertion and deletion in a binary search tree is faster
than in a linked list. We will redesign the video store program so that the video inventory
can be maintained in a binary tree.
-
7/30/2019 Bab 5 - Tree
60/60
Chapter Summary
Binary trees
Binary search trees
Recursive traversal algorithms
Nonrecursive traversal algorithms
AVL trees