lecture7 data structure(tree)

Post on 19-May-2015

1.189 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Binary trees Binary search trees AVL trees B trees Applications

TRANSCRIPT

1

Tree(Data Structure)

Lecture 7

Abdisalam Issa-Salwe

Taibah University College of Computer Science & Engineering

Computer Science Department

2

Outline

� Binary trees

� Binary search trees

� AVL trees

� B trees

� Applications

2

3

Tree ADT

A tree is a collection (may be empty) of nodes, containing:

� a distinguished node called the root r,

� zero or more non-empty subtrees T1, T2, …, Tk,

� A directed edge from the root r to the root of each

subtree. root

T1

T2

Tk

4

Terminologies

rootparent

children

grandchildren

siblings

subtrees

3

5

Terminologies

length of path

path

ancestor

length of path from the root

descendant

depth

6

Terminologies

height

4

7

Tree: Implementationclass TreeNode

{

Object element;

TreeNode firstChild;

TreeNode nextSibling;

}

nextsibling

nextSibling=null

nextSibling=null

nextSibling=null

firstChild=nullfirstChild=null

firstChild=nullfirstChild=null

8

Binary Trees

� A tree in which no node can have more

than 2 children.

5

9

Binary Tree: Implementation

Class BinaryNode

{

Object element;

BinaryNode left;

BinaryNode right;

}

9

node.element=9node.left=nullnode.right=null

5

node.element=5node.left=node node.right=null

3

node.element=3node.left=node node.right=node

10

Binary Tree: Implementation

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

}

6

11

Binary Search Trees

Properties of a binary search tree T

1. T is a binary tree.

2. For each node n in T, whose left subtree

is Tl and right subtree is Tr,

• the item in each node in Tl is smaller than the item in n.

• the item in each node in Tr is larger than the item in n.

12

Example

8

3

42

61

75

11

129

7

13

Binary Search Trees

public class BinarySearchTreepublic class BinarySearchTreepublic class BinarySearchTreepublic class BinarySearchTree

{{{{ public BinarySearchTree( )public BinarySearchTree( )public BinarySearchTree( )public BinarySearchTree( )

{ root = null;{ root = null;{ root = null;{ root = null; }}}}

public void insert( Comparable x )public void insert( Comparable x )public void insert( Comparable x )public void insert( Comparable x )

{ root = insert( x, root ); { root = insert( x, root ); { root = insert( x, root ); { root = insert( x, root ); }}}}

public void remove( Comparable x )public void remove( Comparable x )public void remove( Comparable x )public void remove( Comparable x )

{ root = remove( x, root ); { root = remove( x, root ); { root = remove( x, root ); { root = remove( x, root ); }}}}

public Comparable find( Comparable x )public Comparable find( Comparable x )public Comparable find( Comparable x )public Comparable find( Comparable x )

{ return elementAt( find( x, root ) ); }{ return elementAt( find( x, root ) ); }{ return elementAt( find( x, root ) ); }{ return elementAt( find( x, root ) ); }

public void makeEmpty( )public void makeEmpty( )public void makeEmpty( )public void makeEmpty( )

{ root = null; { root = null; { root = null; { root = null; }}}}

............

private BinaryNode root;private BinaryNode root;private BinaryNode root;private BinaryNode root; }}}}

14

FIND

8

3

42

61

75

11

129

Find 6<

>

>

8

15

Method find

private BinaryNode find

( Comparable x, BinaryNode t )

{

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

}

16

INSERT

8

3

42

61

75

11

129

Insert 10

10

>

<

10

9

17

Method insert

private BinaryNode insert

( Comparable x, BinaryNode t )

{ if( t == null )

t = new BinaryNode( x, null, null );

else if( x.compareTo( t.element ) < 0 )

t.left = insert( x, t.left );

else if( x.compareTo( t.element ) > 0 )

t.right = insert( x, t.right );

else ; // Duplicate; do nothing

return t;

}

18

FindMax, FindMin

8

3

42

61

75

11

129

min

max

10

19

Methods findMin & findMax

private BinaryNode findMin( BinaryNode t )

{ if( t == null )

return null;

else if( t.left == null )

return t;

return findMin( t.left );

}

private BinaryNode findMax( BinaryNode t )

{ if( t != null )

while( t.right != null )

t = t.right;

return t;

}

20

REMOVE

11

3

72

51

64

13

1412

Remove 7

9

10

11

21

REMOVE

11

3

72

1

13

1412

Remove 7

9

108

22

REMOVE

11

3

72

1

13

1412

Remove 7

5

64

12

23

Method Removeprivate BinaryNode remove(Comparable x,BinaryNode t)

{ 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) // 2 child

{ t.element = findMin(t.right).element;

t.right = remove(t.element, t.right);

}

else

t = (t.left != null) ? t.left : t.right;

return t;

}

24

AVL Trees

� An AVL tree is a binary search tree with

a balance condition. A self-balancing

binary search tree.

� AVL tree is named after G.M. Adelson-

Velskii and E.M. Landis.

Balance condition

� For every node in the tree, the height of

the left & right subtrees can differ by at

most 1.

13

25

AVL Trees (cont…)

� The balance factor of a node is the height of its left subtree minus the height of its right subtree (sometimes opposite) and a node with balance factor 1, 0, or −1 is considered balanced.

� A node with any other balance factor is considered unbalanced and requires rebalancing the tree.

� The balance factor is either stored directly at each node or computed from the heights of the subtrees.

26

AVL Trees

11

3

72

1

13

1412

5 8

11

3

72

1

13

1412

5

64AVL tree

not AVL tree

14

27

Single Right Rotation

k2

k1

Xh+1

Yh

Zhk2

k1

Xh+1 Yh Zh

28

Single Left Rotation

k2

Xh

k1

Zh+1

Yh

k1

Zh+1

k2

Xh Yh

15

29

Height of AVL Tree

If N is the number of nodes in an AVL tree, the height of the tree, h(N), is approximately 1.44 log(N+2)-.328.

30

Inorder Traversal

+

-

*a

/

dc

-

fe

b

(a – (b * (c / d))) + (e – f)

16

31

Method inorder

public static void inorder(BinaryNode t)

{ if ( t!=null )

{ inorder(t.left);

System.out.println(t.element);

inorder(t.right);

}

}

32

Preorder Traversal

+

-

*a

/

dc

-

fe

b

+ – a * b / c d – e f

17

33

Method preorder

public static void preorder(BinaryNode t)

{ if ( t!=null )

{ System.out.println(t.element);

inorder(t.left);

inorder(t.right);

}

}

34

Postorder Traversal

+

-

*a

/

dc

-

fe

b

a b c d / * – e f – +

18

35

Method postorder

public static void postorder (BinaryNodet)

{ if ( t!=null )

{ inorder(t.left);

inorder(t.right);

System.out.println(t.element);

}

}

36

B tree� N-ary tree

� Increase the breadth of trees to decrease the height

� Used for indexing of large amount of data (stored in disk)

19

37

B tree (cont…)

� Unlike a binary-tree, each node of a b-tree may have a variable number of keys and children.

� The keys are stored in non-decreasing order.

� Each key has an associated child that is the root of a subtree containing all nodes with keys less than or equal to the key but greater than the preceeding key.

� A node also has an additional rightmost child that is the root for a subtree containing all keys greater than any keys in the node.

38

Example

12 52 78

83 91

60 6919 26 37 46

4 8

012

70717677

7980818283

858690

9395979899

5456575960

61626667

13141719

20212226

27283135

384445

4950

567

891112

20

39

B tree (cont…)

� A b-tree has a minumum number of allowable children for each node known as the minimization factor.

� If t is this minimization factor, every node must have at least t - 1 keys.

� Under certain circumstances, the root node is allowed to violate this property by having fewer than t - 1 keys.

� Every node may have at most 2t - 1 keys or, equivalently, 2t children.

40

Height of B-Tree

� For n greater than or equal to one, the

height of an n-key b-tree T of height h with

a minimum degree t greater than or equal

to 2,

21

41

Properties of B Trees

For an M-ary B tree:

� The root has up to M children.

� Non-leaf nodes store up to M-1 keys, and

have between M/2 and M children, except

the root.

� All data items are stored at leaves.

� All leaves have to same depth, and store

between L/2 and L data items.

42

Search

12 52 78

83 91

60 6919 26 37 46

4 8

012

70717677

7980818283

858690

9395979899

5456575960

61626667

13141719

20212226

27283135

384445

4950

567

891112

Search for 66

22

43

InsertInsert 55

12 52 78

83 91

60 6919 26 37 46

4 8

012

70717677

7980818283

858690

9395979899

5456575960

61626667

13141719

20212226

27283135

384445

4950

567

891112

Split leave

44

Insert

12 52 78

83 91

60 6919 26 37 46

4 8

012

70717677

7980818283

858690

9395979899

5456575960

61626667

13141719

20212226

2728313536

384445

4950

567

891112

Insert 32Split leave

Insert key 31

Insert key 31

23

45

B+ tree

� B+ tree or B plus tree is a type of tree which represents sorted data in a way that allows for efficient insertion, retrieval and removal of records, each of which is identified by a key.

� It is a dynamic, multilevel index, with maximum and minimum bounds on the number of keys in each index segment (usually called a "block" or "node").

� In a B+ tree, in contrast to a B-tree, all records are stored at the leaf level of the tree; only keys are stored in interior nodes.

46

B+ tree (cont…)

� The primary value of a B+ tree is in storing data for efficient retrieval in a block-oriented storage context — in particular, file systems.

� This is primarily because unlike binary search trees, B+ trees have very high fanout (typically on the order of 100 or more), which reduces the number of I/O operations required to find an element in the tree.

24

47

A simple B+ tree example linking the keys 1–7 to data values d1-d7. The linked list (red) allows rapid in-order traversal.

48

References

� Abdisalam Issa-Salwe, Taibah University, Madinah, Saudi Arabia.

top related