Download Tree

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Binary Search Tree
• A kind of binary tree
• Each node stores an item
• For every node X, all items in its left subtree are
smaller than X and all items in its right subtree
are larger than X
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
1
Binary Search Tree
6
2
1
8
4
3
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
2
Algorithms for Binary Search Tree
6
2
1
8
4
3
• Most algorithms for binary search trees use
recursive function.
• Recursively apply the same function on either
left or right subtree, depending on the value
stored in the root.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
3
Binary Search Tree: Find
find 3
<
2 >
1
<
3
6
8
4
Recursively finds the node that
contain X in either left or right
subtree.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
4
Binary Search Tree: Insert
6
2
1
6
2
8
4
3
insert 5
1
8
4
3
5
Recursively inserts X into either left or right subtree.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
5
Delete (with one child)
6
2
1
6
2
8
4
3
delete 4
1
8
4
3
Recursively delete X from either left or right subtree.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
6
Delete (with two children)
6
2
1
delete 2
6
8
5
3
3
8
1
5
delete (3)
3
4
4
Replace data of the node (2) with the smallest data of its right subtree
(3) and delete that node (3) from the right subtree
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
7
Comparable Interface
• Read section 1.4.3 and B.2.3
• Searching and sorting algorithms work on Objects that
can be compared to each other. In other words, these
Objects must have some method for comparing.
• An interface declares a set of methods that a class that
implements the interface must have.
• Comparable interface declares compareTo( ) method.
• Searching and sorting algorithms work on any Objects
that implement Comparable interface.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
8
compareTo( )
• int compareTo( Object rhs ) returns negative
if this Object is less than rsh, zero if they are
equal, and positive if this Object is greater than
rhs
• String and wrapper classes such as Integer all
implement Comparable interface and therefore
have compareTo( ) method.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
9
public interface Comparable
{
int compareTo( Comparable rhs );
}
public class MyInterger implements Comparable
{
public int compareTo( Comparable rhs )
{
return value < ((MyInteger)rhs).value ? -1 :
value == ((MyInteger)rhs).value ? 0 : 1;
}
...
...
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
10
Binary Search Tree
class BinaryNode
{
// Constructors
BinaryNode( Comparable theElement )
{
this( theElement, null, null );
}
BinaryNode( Comparable theElement, BinaryNode lt, BinaryNode rt )
{
element = theElement;
left = lt;
right = rt;
}
// Friendly data; accessible by other package routines
Comparable element;
// The data in the node
BinaryNode left;
// Left child
BinaryNode right;
// Right child
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
11
public class BinarySearchTree
{
public BinarySearchTree( ) { root = null; }
public void
public void
public Comparable
public Comparable
public Comparable
public void
public boolean
public void
insert( Comparable x )
remove( Comparable x )
findMin( )
findMax( )
find( Comparable x )
makeEmpty( )
isEmpty( )
printTree( )
private Comparable elementAt( BinaryNode t )
private BinaryNode insert( Comparable x, BinaryNode t )
private BinaryNode remove( Comparable x, BinaryNode t )
private BinaryNode findMin( BinaryNode t )
private BinaryNode findMax( BinaryNode t )
private BinaryNode find( Comparable x, BinaryNode t )
private void
printTree( BinaryNode t )
private BinaryNode root;
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
12
public static void main( String [ ] args )
{
final int NUMS = 4000;
BinarySearchTree t = new BinarySearchTree( );
for( int i = 1; i < NUMS; i++ )
t.insert( new MyInteger( i ) );
for( int i = 1; i < NUMS; i+= 2 )
t.remove( new MyInteger( i ) );
if( NUMS < 40 ) t.printTree( );
if( ((MyInteger)(t.findMin( ))).intValue( ) != 2 ||
((MyInteger)(t.findMax( ))).intValue( ) != NUMS - 2 )
System.out.println( "FindMin or FindMax error!" );
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
13
public void printTree( )
{
if( isEmpty( ) )
System.out.println( "Empty tree" );
else
printTree( root );
}
private void printTree( BinaryNode t )
{
if( t != null )
{
printTree( t.left );
System.out.println( t.element );
printTree( t.right );
}
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
14
public Comparable find( Comparable x )
{
return elementAt( find( x, root ) );
}
private BinaryNode find( Comparable x, BinaryNode t )
{
/* find() recursively finds the node that contain x in either left or right subtree
and returns a reference to that node, and returns null if not found */
if( t == null )
return null;
if( x.compareTo( t.element ) < 0 )
return find( x, t.left );
else if( x.compareTo( t.element ) > 0 )
return find( x, t.right );
else
return t; // Match
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
15
public Comparable findMin( )
{
return elementAt( findMin( root ) );
}
private BinaryNode findMin( BinaryNode t )
{ /* Find the left-most leaf node */
if( t == null )
return null;
else if( t.left == null )
return t;
return findMin( t.left );
}
/* Nonrecursive implementation
private BinaryNode findMin( BinaryNode t )
{
if( t != null )
while ( t.left != null )
t = t.left;
return t;
}
*/
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
16
public Comparable findMax( )
{
return elementAt( findMax( root ) );
}
private BinaryNode findMax( BinaryNode t )
{ /* Find the right-most leaf node */
if( t == null )
return null;
else if( t.right == null )
return t;
return findMax( t.right );
}
/* Nonrecursive implementation
private BinaryNode findMax( BinaryNode t )
{
if( t != null )
while( t.right != null )
t = t.right;
return t;
}
*/
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
17
public void insert( Comparable x )
{
root = insert( x, root );
}
private BinaryNode insert( Comparable x, BinaryNode t )
/* insert() recursively inserts data into either left or right subtree and returns a
reference to the root of the new tree */
{
/* 1*/
if( t == null )
/* 2*/
t = new BinaryNode( x, null, null );
/* 3*/
else if( x.compareTo( t.element ) < 0 )
/* 4*/
t.left = insert( x, t.left );
/* 5*/
else if( x.compareTo( t.element ) > 0 )
/* 6*/
t.right = insert( x, t.right );
/* 7*/
else
/* 8*/
; // Duplicate; do nothing
/* 9*/
return t;
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
18
public void remove( Comparable x )
{ root = remove( x, root );
}
private BinaryNode remove( Comparable x, BinaryNode t )
{ /* remove() recursively removes data from either left or right subtree and returns a
reference to the root of the new tree */
if( t == null )
return t; // Item not found; do nothing
if( x.compareTo( t.element ) < 0 )
t.left = remove( x, t.left );
else if( x.compareTo( t.element ) > 0 )
t.right = remove( x, t.right );
else if( t.left != null && t.right != null ) /* Two children. Replace data of this node with
the smallest data of the right subtree and delete that node from the right subtree. */
{
t.element = findMin( t.right ).element;
t.right = remove( t.element, t.right );
}
else
t = ( t.left != null ) ? t.left : t.right;
return t;
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
19
Lazy Deletion
• When an element is to be deleted, it is left in the tree
and marked as being deleted.
• If there are duplicate items, the variable that keeps the
number of duplicated items is just decremented.
• If a deleted item is reinserted, no need to allocate new
node.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
20
Shape, Height, and Size
• Given height, balanced tree hold maximum number of nodes
• Given number of nodes, balanced tree is the shortest
• When height increases by one, number of nodes in balanced tree
approximately doubles.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
21
Average-Case
• Balanced binary tree: height = log(n+1) - 1
• Average binary tree: height = O(log N)
• After lots of insert/remove operations, binary
search tree becomes imbalance (remove operation
make the left subtree deeper than the right)
• If insert presorted data into binary search tree,
the tree will have only nodes with right child.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
22
Balance Binary Search Trees
First attempt:
Left and right subtree
must have the same
height
Second attempt:
Every node must have
left and right subtrees
of the same height
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
23
AVL Tree
• AVL Tree = Adelson-Velskii and Landis Tree
• A binary search tree with balance condition
• Self-adjusting to maintain balance condition
• Ensure that the depth of the tree is O(log N)
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
24
AVL Tree
• For every node, the height of the left and right
subtrees can differ by at most 1
• Height information is kept for each node
• Height is at most 1.44log(N+2) - .328
or slightly more than logN
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
25
An AVL Tree
Not an AVL Tree
8
8
12
4
2
6
5
10
12
4
2
6
5
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
7
26
Smallest AVL Tree of Height 5
Minimum number of nodes in an AVL tree of height h
is S(h) = S(h-1)+S(h-2)+1, S(0)=1, S(1) = 2
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
27
Violation of the AVL Property
• Inserting a node could violate the AVL property
if it cause a height imbalance (two subtrees’
height differ by two)
• Solution: The deepest node with imbalanced
subtrees must be rebalanced
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
28
Insertion Causes Imbalance
8
12
4
2
6
5
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
29
Four Cases of Violation
8
12
4
2
8
12
4
10
6
1
14
15
Insert into left subtree of the left child
Insert into right subtree of the right child
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
30
Four Cases of Violation
8
8
12
4
2
12
4
10
6
14
11
5
Insert into right subtree of the left child
Insert into left subtree of the right child
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
31
Four Cases of Violation
4
2
1
4
4
6
4
2
7
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
6
3
5
32
Single Rotation
Before
k1
2
8
4
k2
6
Y
Z
After
2
12
4
k1
1
X
8
k2
6
12
Y
Z
1
X
Insertion of 1 causes X to be one level deeper than Y and two level deeper than Z.
The violation occurs at k2. The rotation maintains k1 < Y < k2. X is lifted up one
level, Y remains at the same level, and Z is lowered down one level. The new height
is the same as before the insertion.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
33
Single Rotation
Before
After
8
12
12
4
10
8
14
4
14
10
15
15
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
34
Single Rotation (this one does not work)
Before
After
4
8
4
2
6
12
2
Z
X
8
6
X
5
5
12
Z
Y
Y
Y remains at the same level, so it does not work.
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
35
Double Rotation
Before
After double rotation
k3
k2
Z
k1
X
k2
Y1
k3
k1
X
Y1
Y2
Z
Y2
An insertion into Y1 or Y2 causes violation at k3.
k1 < Y1 < k2 < Y2 < k3
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
36
Double Rotation
After first rotation
Before
8
8
12
4
2
6
6
5
12
4
2
5
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
37
Double Rotation
After first rotation
After second rotation
8
6
12
8
4
2
4
2
6
5
12
5
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
38
Double Rotation
After first rotation
Before
8
8
12
4
10
4
10
12
14
11
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
11
14
39
Double Rotation
After first rotation
After second rotation
8
4
10
10
8
12
11
4
12
11
14
14
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
40
class AvlNode
{
Comparable element;
AvlNode
left;
AvlNode
right;
int
height;
AvlNode( Comparable theElement )
{ this( theElement, null, null ); }
AvlNode( Comparable theElement, AvlNode lt, AvlNode rt )
{ element = theElement; left = lt; right = rt; height = 0; }
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
41
private AvlNode insert( Comparable x, AvlNode t )
{
if( t == null )
t = new AvlNode( x, null, null );
else if( x.compareTo( t.element ) < 0 )
{ /* insert into the left subtree */
t.left = insert( x, t.left );
if(height( t.left ) - height( t.right ) == 2 )
if( x.compareTo( t.left.element ) < 0 )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
42
else if( x.compareTo( t.element ) > 0 )
{ /* insert into the right subtree */
t.right = insert( x, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( x.compareTo( t.right.element ) > 0 )
t = roteWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else
;
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
43
private static AvlNode rotateWithLeftChild( AvlNode k2 )
{
AvlNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
private static AvlNode doubleWithLeftChild( AvlNode k3 )
{
k3.left = rotateWithRightChild( k3.left );
return rotateWithLeftChild( k3 );
}
2110211 Intro. to Data Structures Chapter 4 Tree Veera
Muangsin, Dept. of Computer Engineering, Chulalongkorn
44
Related documents