Trees
This presentation is the property of its rightful owner.
Sponsored Links
1 / 163

Trees PowerPoint PPT Presentation


  • 163 Views
  • Uploaded on
  • Presentation posted in: General

Trees. Chapter 6. Chapter Objectives. Tree structure Tree recursion Tree traversing Tree examples binary trees binary search trees heaps Tree implementation linked data structures arrays. Chapter Objectives (cont.). Binary search tree Information store and retrieve efficiently

Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Trees

Trees

Chapter 6


Chapter objectives

Chapter Objectives

  • Tree structure

  • Tree recursion

  • Tree traversing

  • Tree examples

    • binary trees

    • binary search trees

    • heaps

  • Tree implementation

    • linked data structures

    • arrays


Chapter objectives cont

Chapter Objectives (cont.)

  • Binary search tree

    • Information store and retrieve efficiently

  • Huffman tree

    • reduced storage


What is a tree

What is a Tree?

  • Linear structure

    • one predecessor and successor

  • Accessing in O(n)

  • Trees

    • Nonlinear

    • Hierarchical

    • One predecessor

    • Multiple successors


How to use tree structure

How to use Tree structure?

  • Represent hierarchy

    • class hierarchy

    • disk directory and subdirectories

    • family tree

    • State organization

  • Process trees recursively

    • recursive data structures


Binary tree

Binary Tree

  • Each element has two successors

  • Be represented by

    • Arrays

    • Linked data structures

  • Searching

    • O(log n) versus O(n)


Tree terminology and applications

Tree Terminology and Applications

Section 6.1


Trees

Node

dog

cat

wolf

canine


R oot

Root

The node at the very top of a tree is called its root

dog

cat

wolf

canine


B ranches

Branches

The links from a node to its successors are called branches

dog

cat

wolf

canine


C hildren

Children

The successors of a node are called its children

dog

cat

wolf

canine


P arent

Parent

The predecessor of a node is called its parent

dog

cat

wolf

canine


Parent uniqueness

Parent uniqueness

Each node in a tree has exactly one parent except for the root node, which has no parent

dog

cat

wolf

canine


S iblings

Siblings

Nodes that have the same parent are siblings

dog

cat

wolf

canine


L eaf

Leaf

A node that has no children is called a leaf

dog

cat

wolf

canine


Internal and external nodes

Internal and external nodes

External nodes-leaf nodes

Internal nodes-non leaf nodes

dog

cat

wolf

canine


Ancestor descendant relationship

Ancestor-descendant relationship

A generalization of the parent-child relationship is the ancestor-descendant relationship

dog

cat

wolf

canine


Ancestor descendant relationship 1

Ancestor-descendant relationship-1

dog

dog is the parent of cat in this tree

cat

wolf

canine


Ancestor descendant relationship 2

Ancestor-descendant relationship-2

dog

cat is the parent of canine in this tree

cat

wolf

canine


Ancestor descendant relationship 3

Ancestor-descendant relationship-3

dog

cat

wolf

canine is a descendant of cat in this tree

canine


Ancestor descendant relationship 4

Ancestor-descendant relationship-4

dog

dog is an ancestor of canine in this tree

cat

wolf

canine


Subtree

Subtree

dog

cat

wolf

canine

A subtree of a node is a tree whose root is a child of that node


S ubtree 1

Subtree-1

dog

cat

wolf

canine


S ubtree 2

Subtree-2

dog

cat

wolf

canine


The level of a node

The level of a node

The level of a node is determined by its distance from the root

dog

cat

wolf

canine


Level example

Level example

Distance from the root plus 1

dog

Level 1

cat

wolf

Level 2

canine

Level 3


Level is defined recursively

Level is defined recursively

dog

Level 1

cat

wolf

Level 2

canine

Level 3


R ecursive definition of level

Recursive definition of level

  • If node n is the root of tree T, its level is 1

  • If node n is not the root of tree T

    • its level is 1 + the level of its parent

dog

Level 1

cat

wolf

Level 2

canine

Level 3


H eight

Height

the number of nodes in the longest path from the root to a leaf

dog

cat

wolf

canine


Height example

Height example

dog

cat

wolf

The height of this tree is 3

canine


Binary trees

Binary Trees

  • Each node has two subtrees

  • T is a binary tree- either is true

    • T is empty

    • Its root node has two subtrees, TL and TR

      • TL and TR are binary trees


Expression tree

Expression Tree

  • Internal node

    • operator

  • Leaf node

    • Operands

  • Parentheses

    • stored in the tree structure

  • Evaluation

    • from bottom to top

    • from Left to right

(x + y) * ((a + b) / c)


Huffman tree

Huffman Tree?

  • Represents Huffman codes

  • Huffman code

    • different numbers of bits to encode letters

    • fewer bits for high frequently used letters

  • Compressing files


Huffman tree1

Huffman Tree

To form a code, traverse the tree from the root to the chosen character, appending 0 if you turn left, and 1 if you turn right.


Example

Example

Examples:

d : 10110

e : 010


Binary search tree

Binary Search Tree

  • Binary search trees

    • TL precedes TR

  • A formal definition:

    T is a binary search tree with either true

    • T is empty

    • If T is not empty,TLand TR are binary search trees

      • TL< root <TR

dog

cat

wolf

canine


Features

Features

  • Sorted

  • Order is maintained

    • Add a new element

    • Remove an element


Search performance

Search Performance

average O(log n)

Worst case  O(n)


Recursive algorithm for searching a binary tree

Recursive Algorithm for Searching a Binary Tree

if the tree is empty

return null (target is not found)else if the target matches the root node's data

return the data stored at the root nodeelse if the target is less than the root node's data

return the result of searching the left subtree of the rootelse

return the result of searching the right subtree of the root


Full binary tree

Full Binary Tree

All nodes have either 2 children or 0 children

7

1

2

4

5

6

10

0

3

9

12

11

13


Perfect binary trees

Perfect Binary Trees

Full binary tree

Height n with exactly 2n – 1 nodes

3

1

5

0

2

4

6


Complete binary tree

Complete Binary Tree

A perfect binary tree through level n – 1

Extra leaf nodes at level n

All toward the left

3

1

5

0

2

4


General trees

General Trees

Can have any number of subtrees


General tree and binary tree

General Tree and Binary Tree

A general tree can be represented using a binary tree


Tree traversals

Tree Traversals

Section 6.2


Tree traversals1

Tree Traversals

  • Tree traversal

    • Walking through the tree in a prescribed order

  • Three types of traversal

    • Inorder

    • Preorder

    • Postorder


Three types of tree traversals

Three types of Tree Traversals

  • Preorder

    • RootTLTR

  • Inorder

    • TLrootTR

  • Postorder

    • TLTRroot


Euler tour

Euler Tour

Keep the tree always at the left


Euler tour1

Euler Tour

  • Preorder traversal

    • a b d g e h c f i j


Inorder traversal

Inorder Traversal

The sequence isd g b h e a i f j c


Postorder traversal

Postorder Traversal

The sequence isg d h e b i j f c a


Inorder traversal example 1

Inorder Traversal Example-1

canine, cat, dog, wolf

Increasing order

dog

cat

wolf

canine


Inorder traversals example 2

Inorder Traversals-Example 2

Results in the sequencex + y * a + b / c

The infix form

(x + y) * ((a + b) / c)

*

+

/

x

a

y

b

+

c


Postorder traversal example 3

Postorder Traversal-Example 3

Result in the sequencex y + a b + c / *

postfix

*

+

/

x

a

y

b

+

c


Preorder traversal example 4

Preorder Traversal-Example 4

Result in the sequence* + x y / + a b c

Prefix form of the expression

Operators precede operands

*

+

/

x

a

y

b

+

c


Implementing a binarytree class

Implementing a BinaryTreeClass

Section 6.3


Node e class

Node<E>Class

Data part

Links


Node e class code

Node<E>Class Code

protected static class Node<E> implements Serializable {

protected E data;

protected Node<E> left;

protected Node<E> right;

public Node(E data) {

this.data = data;

left = null;

right = null;

}

public String toString() {

return data.toString();

}

}

Node<E> is declared as an inner class within BinaryTree<E>


Protected feature

Protected feature

Node<E> is declared protected. This way we can use it as a superclass.


Expression binarytree e

Expression BinaryTree<E>


Binarytree e class bt

BinaryTree<E> Class: bT

Assuming the tree is referenced by variable bT (type BinaryTree) then . . .


D ata field

Data field

bT.root.data references the Characterobject storing '*'


L eft reference

Left reference

bT.root.left references the left subtree of the root


R ight reference

Right reference

bT.root.right references the right subtree of the root


Root of t r

Root of TR

bT.root.right.data references the Characterobject storing '/'


Binarytree e class field method

BinaryTree<E> Class: Field, Method


Binarytree e class code

BinaryTree<E> Class Code

Class heading and data field declarations:

import java.io.*;

public class BinaryTree<E> implements Serializable {

// Insert inner class Node<E> here

protected Node<E> root;

. . .

}


Serializable interface

Serializable Interface

  • No methods are defined

  • It provides a marker for classes

    • write to a binary file using

      ObjectOutputStream

    • read from a binary file using

      ObjectInputStream


Constructors empty one node

Constructors: empty/one node

The no-parameter constructor:

public BinaryTree() {

root = null;

}

with a given node at the root:

protected BinaryTree(Node<E> root) {

this.root = root;

}


Constructor full parameters

Constructor: full parameters

The constructor that builds a tree from a data value and two trees:

public BinaryTree(E data, BinaryTree<E> leftTree,

BinaryTree<E> rightTree) {

root = new Node<E>(data);

if (leftTree != null) {

root.left = leftTree.root;

} else {

root.left = null;

}

if (rightTree != null) {

root.right = rightTree.root;

} else {

root.right = null;

}

}


Getleftsubtree and getrightsubtree methods

getLeftSubtreeandgetRightSubtreeMethods

public BinaryTree<E> getLeftSubtree() {

if (root != null && root.left != null) {

return new BinaryTree<E>(root.left);

} else {

return null;

}

}

  • getRightSubtreemethod is symmetric


Isleaf method

isLeaf Method

public boolean isLeaf() {

return (root.left == null && root.right == null);

}


Tostring method

toString Method

Preorder traversal of a tree

public String toString() {

StringBuilder sb = new StringBuilder();

preOrderTraverse(root, 1, sb);

return sb.toString();

}


Preordertraverse method

preOrderTraverse Method

private void preOrderTraverse(Node<E> node, int depth, StringBuilder sb) {

for (int i = 1; i < depth; i++) {

sb.append(" "); // indentation

}

if (node == null) {

sb.append("null\n");

} else {

sb.append(node.toString());

sb.append("\n");

preOrderTraverse(node.left, depth + 1, sb);

preOrderTraverse(node.right, depth + 1, sb);

}

}


Preordertraverse example

preOrderTraverse Example

*

+

x

null

null

y

null

null

/

a

null

null

b

null

null

*

+

/

x

y

a

b

(x + y) * (a / b)


Reconstruct a binary tree from a string

Reconstruct a Binary Treefrom a string

  • Scanner

    • read the individual lines created by preOrderTraverse

  • Read a line that represents information at the root

  • Remove the leading and trailing spaces using String.trim

  • if it is "null"

  • return null

    else

  • recursively read the left child

  • recursively read the right child

  • return a tree consisting of the root and the two children


Reading a binary tree code

Reading a Binary Tree Code

public static BinaryTree<String> readBinaryTree(Scanner scan) {

String data = scan.next();

if (data.equals("null")) {

return null;

} else {

BinaryTree<String> leftTree = readBinaryTree(scan);

BinaryTree<String> rightTree = readBinaryTree(scan);

return new BinaryTree<String>(data, leftTree, rightTree);

}

}


Using objectoutputstream and objectinputstream

Using ObjectOutputStreamandObjectInputStream

  • ObjectOutputStream

    • write a Serializableobject to a file


Use objectoutputstream

UseObjectOutputStream

  • To write a Serializable object to a file:

    try {

    ObjectOutputStream out =

    new ObjectOutputStream(new FileOutputStream(filename));

    out.writeObject(nameOfObject);

    } catch (Exception ex) {

    ex.printStackTrace();

    System.exit(1);

    }


Use objectinputstream

UseObjectInputStream

  • To read a Serializable object from a file:

    try {

    ObjectInputStream in =

    new ObjectInputStream(new FileInputStream(filename));

    objectName = (objectClass)in.readObject();

    } catch (Exception ex) {

    ex.printStackTrace();

    System.exit(1);

    }


Using tips

Usingtips

Do not recompile the Java source file for a class after an object of that class has been serialized

Even if you didn't make any changes to the class, the resulting .class file associated with the serialized object will have a different class signature

When you attempt to read the object, the class signatures will not match, and you will get an exception


Binary search tree1

Binary Search Tree

Section 6.4


Review

Review

  • Recall the definition of a binary search tree:

    A set of nodes T is a binary search tree if either of the following is true

    • T is empty

    • If T is not empty, its root node has two subtrees TLand TR

      • TLand TR are binary search trees

      • TL<Root<TR


Example1

Example


Search a binary search tree

Search a Binary Search Tree

  • if the root is null

  • the item is not in the tree; return null

  • Compare the value of target with root.data

  • if they are equal

  • the target has been found; return the data at the root

    else if the target is less than root.data

  • return the result of searching the left subtree

    else

  • return the result of searching the right subtree


Searching example

Searching Example

Searching for "kept"


Searching performance

Searching Performance

  • Generally O(log n)

  • Not very full

    • performance will be worse

  • With only right subtrees

    • performance is O(n)


Interface searchtree e method

InterfaceSearchTree<E>: Method


Binarysearchtree e class

BinarySearchTree<E> Class


Implementing find methods

ImplementingfindMethods


Insertion into a binary search tree

Insertion into a Binary Search Tree


Add starter

addstarter

/** Starter method add.

pre: The object to insert must implement the

Comparable interface.

@param item The object being inserted

@return true if the object is inserted, false

if the object already exists in the tree

*/

public boolean add(E item) {

root = add(root, item);

return addReturn;

}


Add method code

addMethod code

/** Recursive add method.

post: The data field addReturn is set true if the item is added to

the tree, false if the item is already in the tree.

@paramlocalRootThe local root of the subtree

@param item The object to be inserted

@return The new local root that now contains the

inserted item

*/

private Node<E> add(Node<E> localRoot, E item) {

if (localRoot == null) {

// item is not in the tree — insert it.

addReturn = true;

return new Node<E>(item);

} else if (item.compareTo(localRoot.data) == 0) {

// item is equal to localRoot.data

addReturn = false;

return localRoot;

} else if (item.compareTo(localRoot.data) < 0) {

// item is less than localRoot.data

localRoot.left = add(localRoot.left, item);

return localRoot;

} else {

// item is greater than localRoot.data

localRoot.right = add(localRoot.right, item);

return localRoot;

}

}


Removal from a binary search tree

Removal from a binary search tree

  • No children

    • simply delete the reference to the item

  • Only one child

    • change the reference to the item so that it references the item’s only child


Example2

Example


W ith two children

With two children

  • Inorder predecessor

    • Replace it with the largest item in its left subtree


Example in right subtree

Example in right subtree


Algorithm for removing

Algorithm for Removing


Implementing the delete method

Implementing the deleteMethod

Listing 6.5 Binary Search Tree Code


Method findlargestchild

MethodfindLargestChild


Heaps and priority queues

Heaps and Priority Queues

Section 6.5


Trees

Heap

  • A complete binary tree with the following properties

    • the root is the smallest item in the tree

    • Every nonempty subtreeis a heap


Inserting an item into a heap case 1

Inserting an Item into a Heap Case 1

6

18

29

20

39

66

28

37

26

76

32

74

89


Inserting an item into a heap case 2

Inserting an Item into a Heap Case 2

6

18

29

20

39

66

28

8

37

26

76

32

74

89


Inserting an item into a heap case 2 exchange with its parent

Inserting an Item into a HeapCase 2-exchange with its parent

6

18

29

20

39

8

28

66

37

26

76

32

74

89


Inserting an item into a heap case 2 exchange again

Inserting an Item into a HeapCase 2-exchange again

6

18

8

20

39

29

28

66

37

26

76

32

74

89


Inserting an item into a heap case 2 over

Inserting an Item into a HeapCase 2-over

6

18

8

20

39

29

28

66

37

26

76

32

74

89


Removing an item from a heap

Removing an Item from a Heap

6

18

8

20

39

29

28

66

37

26

76

32

74

89


Removing an item from a heap remove root 6

Removing an Item from a Heap-remove root ‘6’

6

18

8

20

39

29

28

66

37

26

76

32

74

89


Removing an item from a heap replace with last item 66

Removing an Item from a Heap-replace with last item ‘66’

66

18

8

20

39

29

28

37

26

76

32

74

89


Removing an item from a heap move lih down

Removing an Item from a Heap-move LIH down

8

18

66

20

39

29

28

37

26

76

32

74

89


Removing an item from a heap move down again root 6 is deleted

Removing an Item from a Heap-Move down again, root ‘6’ is deleted

8

18

29

20

39

66

28

37

26

76

32

74

89


Removing an item from a heap cont

Removing an Item from a Heap (cont.)

8

18

29

20

39

66

28

37

26

76

32

74

89


How to implement a heap

How to Implement a Heap?

  • Aheap is a complete binary tree

    • It can be implemented efficiently using an array/ArrayList rather than a linked data structure


Example of implementing a heap

Example of Implementing a Heap

8

8

29

29

18

18

39

66

66

20

20

28

28

39

37

37

26

26

76

76

32

32

74

74

89

89

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39


Find a node at position p case 1

Find a node at Position ‘p’-case 1

For a node at position p,L. child position: 2p+ 1

R. child position: 2p + 2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Parent

L. Child

R. Child

74

89


Find a node at position p case 2

Find a node at Position ‘p’-case 2

For a node at position p,L. child position: 2p+ 1

R. child position: 2p + 2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Parent

L. Child

R. Child

74

89


Find a node at position p case 3

Find a node at Position ‘p’-case 3

For a node at position p,L. child position: 2p+ 1

R. child position: 2p + 2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Parent

L. Child

R. Child

74

89


Find a node at position p case 4

Find a node at Position ‘p’-case 4

For a node at position p,L. child position: 2p+ 1

R. child position: 2p + 2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Parent

L. Child

R. Child

74

89


Find a node at position p case 5

Find a node at Position ‘p’-case 5

For a node at position p,L. child position: 2p+ 1

R. child position: 2p + 2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Parent

L. Child

R. Child

74

89


Find its parent for a nod at position c

Find its parent for a nodat position ‘c’

A node at position c can find its parent at(c – 1)/2

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

Child

Parent

74

89


Inserting into a heap implemented as an arraylist

Inserting into a Heap Implemented as an ArrayList

1. Insert the new element at the end of the ArrayList and set child to table.size() - 1

8

18

29

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

66

28

37

26

76

74

89

8

18

29

39

37

26

76

32

74

89


Inserting into a heap implemented as an arraylist cont

Inserting into a Heap Implemented as an ArrayList (cont.)

1. Insert the new element at the end of the ArrayList and set child to table.size() - 1

6

18

29

8

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

66

28

37

26

76

74

89

8

6

18

29

39

37

26

76

32

Child

74

89


Inserting into a heap implemented as an arraylist cont1

Inserting into a Heap Implemented as an ArrayList(cont.)

2. Set parent to (child – 1)/ 2

6

18

29

8

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

66

28

37

26

76

74

89

8

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont2

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

29

8

20

39

66

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

66

28

37

26

76

74

89

8

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont3

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

29

66

20

39

8

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

8

28

37

26

76

74

89

66

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont4

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

29

66

20

39

8

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

8

28

37

26

76

74

89

66

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont5

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

29

66

20

39

8

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

8

28

37

26

76

74

89

66

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont6

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

29

66

20

39

8

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

8

28

37

26

76

74

89

66

6

18

29

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont7

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

8

66

20

39

29

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

29

28

37

26

76

74

89

66

6

18

8

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont8

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

8

66

20

39

29

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

29

28

37

26

76

74

89

66

6

18

8

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont9

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

8

66

20

39

29

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

29

28

37

26

76

74

89

66

6

18

8

39

37

26

76

32

Parent

Child

74

89


Inserting into a heap implemented as an arraylist cont10

Inserting into a Heap Implemented as an ArrayList(cont.)

3. while (parent >= 0 and table[parent] > table[child])

Swap table[parent] and table[child]

Set child equal to parent

Set parent equal to (child-1)/2

6

18

8

66

20

39

29

28

0

1

2

3

4

5

6

7

8

9

10

11

12

13

20

32

29

28

37

26

76

74

89

66

6

18

8

39

37

26

76

32

74

89


Removal from a heap implemented as an arraylist

Removal from a Heap Implemented as anArrayList

Removing an Element from a Heap Implemented as an ArrayList

1. Remove the last element (i.e., the one at size() – 1) and set the item at 0 to this value.

2. Set parent to 0.

3. while (true)

4. Set leftChild to (2 * parent) + 1 and rightChild to leftChild + 1.

5. if leftChild >= table.size()

6. Break out of loop.

7. Assume minChild (the smaller child) is leftChild.

8. if rightChild < table.size() and

table[rightChild] < table[leftChild]

9. Set minChild to rightChild.

10. if table[parent] > table[minChild]

11. Swap table[parent] and table[minChild].

12. Set parent to minChild.

else

13. Break out of loop.


Performance of the heap

Performance of the Heap

  • remove

    • traces a path from the root to a leaf

  • insert

    • traces a path from a leaf to the root

  • This requires at most h steps

  • The largest full tree of height h has 2h-1 nodes

  • The smallest complete tree of height h

    • 2(h-1) nodes

  • Both insert and remove are O(log n)


Priority queue and heap

Priority Queue and Heap

  • Priority queue

    • Implemented by a heap

    • Used in non-FIFO case

  • The heap is not very useful as an ADT on its own

    • Priority Queue

    • Heapsort


Priority queues application

Priority Queues: application

Print out a short document

Short process is served first


Insertion into a priority queue

Insertion into a Priority Queue


Priorityqueue class

PriorityQueueClass

Java provides a PriorityQueue<E> class that implements the Queue<E> interface given in Chapter 4.


Using a heap as the basis of a priority queue

Using a Heap as the Basis of a Priority Queue

  • The smallest item always is removed first

  • Heap is used to implement a priority queue

    • heap insertion and removal is O(log n)

  • Implementation with ArrayList


Design of a kwpriorityqueue class using arraylist

Design of a KWPriorityQueueClass-using ArrayList


Design of a kwpriorityqueue class

Design of a KWPriorityQueueClass

import java.util.*;

/** The KWPriorityQueue implements the Queue interface

by building a heap in an ArrayList. The heap is structured

so that the "smallest" item is at the top.

*/

public class KWPriorityQueue<E> extends AbstractQueue<E>

implements Queue<E> {

// Data Fields

/** The ArrayList to hold the data. */

private ArrayList<E> theData;

/** An optional reference to a Comparator object. */

Comparator<E> comparator = null;

// Methods

// Constructor

public KWPriorityQueue() {

theData = new ArrayList<E>();

}

. . .


Offer method

offerMethod

/** Insert an item into the priority queue.

pre: The ArrayListtheData is in heap order.

post: The item is in the priority queue and

theData is in heap order.

@param item The item to be inserted

@throws NullPointerException if the item to be inserted is null.

*/

@Override

public boolean offer(E item) {

// Add the item to the heap.

theData.add(item);

// child is newly inserted item.

int child = theData.size() - 1;

int parent = (child - 1) / 2; // Find child’s parent.

// Reheap

while (parent >= 0 && compare(theData.get(parent),

theData.get(child)) > 0) {

swap(parent, child);

child = parent;

parent = (child - 1) / 2;

}

return true;

}


Poll method

pollMethod

/** Remove an item from the priority queue

pre: The ArrayListtheData is in heap order.

post: Removed smallest item, theData is in heap order.

@return The item with the smallest priority value or null if empty.

*/

@Override

public E poll() {

if (isEmpty()) {

return null;

}

// Save the top of the heap.

E result = theData.get(0);

// If only one item then remove it.

if (theData.size() == 1) {

theData.remove(0);

return result;

}


Poll method cont

pollMethod(cont.)

/* Remove the last item from the ArrayList and place it into

the first position. */

theData.set(0, theData.remove(theData.size() - 1));

// The parent starts at the top.

int parent = 0;

while (true) {

int leftChild = 2 * parent + 1;

if (leftChild >= theData.size()) {

break; // Out of heap.

}

int rightChild = leftChild + 1;

int minChild = leftChild; // Assume leftChild is smaller.

// See whether rightChild is smaller.

if (rightChild < theData.size()

&& compare(theData.get(leftChild),

theData.get(rightChild)) > 0) {

minChild = rightChild;

}

// assert: minChild is the index of the smaller child.

// Move smaller child up heap if necessary.

if (compare(theData.get(parent),

theData.get(minChild)) > 0) {

swap(parent, minChild);

parent = minChild;

} else { // Heap property is restored.

break;

}

}

return result;

}


Other methods

Other Methods

The iterator and size methods are implemented via delegation to the corresponding ArrayList methods

Method isEmpty tests whether the result of calling method size is 0 and is inherited from class AbstractCollection

The implementations of methods peek and remove are left as exercises


Using a comparator

Using a Comparator

To use an ordering that is different from the natural ordering, provide a constructor that has a Comparator<E> parameter

/** Creates a heap-based priority queue with the specified initial

capacity that orders its elements according to the specified

comparator.

@param cap The initial capacity for this priority queue

@param comp The comparator used to order this priority queue

@throws IllegalArgumentException if cap is less than 1

*/

public KWPriorityQueue(Comparator<E> comp) {

if (cap < 1)

throw new IllegalArgumentException();

theData = new ArrayList<E>();

comparator = comp;

}


Compare method

compareMethod

If data field comparator references a Comparator<E> object, method compare delegates the task to the objects compare method

If comparator is null, it will delegate to method compareTo


Compare method cont

compareMethod(cont.)

/** Compare two items using either a Comparator object’s compare method

or their natural ordering using method compareTo.

pre: If comparator is null, left and right implement Comparable<E>.

@param left One item

@param right The other item

@return Negative int if left less than right,

0 if left equals right,

positive int if left > right

@throws ClassCastException if items are not Comparable

*/

private int compare(E left, E right) {

if (comparator != null) { // A Comparator is defined.

return comparator.compare(left, right);

} else { // Use left’s compareTo method.

return ((Comparable<E>) left).compareTo(right);

}

}


Printdocuments example

PrintDocumentsExample

Define documents to be printed on a printer

Order documents by both size and time submitted

In the client program, use

Queue printQueue =

new PriorityQueue(new ComparePrintDocuments());


Printdocuments example cont

PrintDocumentsExample (cont.)


Huffman trees

Huffman Trees

Section 6.6 (Optional)


Huffman trees1

Huffman Trees

  • Can be implemented using

    • a binary tree and a PriorityQueue

  • Compare to Unicode with “go eagles”

    • Unicode144 bits

    • Huffman coding38 bits


Symbol usage frequency

Symbol Usage Frequency


Huffman trees2

Huffman Trees


Building a custom huffman tree

Building a Custom Huffman Tree

  • Purpose

    • Build a custom Huffman tree for a file

  • Input

    • an array of objects

    • each object contains

      • a reference to a symbol occurring in that file

      • the frequency of occurrence for the symbol in that file


Building a custom huffman tree analysis

Building a Custom Huffman Tree-Analysis

  • Each node will have storage for two data items:

    • the weight of the node and

    • the symbol associated with the node

  • All symbols will be stored in leaf nodes

  • For not leaf nodes

    • the symbol part has no meaning

  • The weight of a leaf node

    • the frequency of the symbol stored at that node

  • The weight of an interior node

    • the sum of frequencies of all leaf nodes in the subtree rooted at the interior node


Building a custom huffman tree analysis cont

Building a Custom Huffman TreeAnalysis (Cont.)

  • The key data structure

    • Priority queue

  • Store individual symbols and subtrees of multiple symbols in order by their priority (frequency of occurrence)


Building a custom huffman tree example

Building a Custom Huffman TreeExample


Building a custom huffman tree example cont

Building a Custom Huffman Tree Example (cont.)


Design algorithm

Design Algorithm

Algorithm for Building a Huffman Tree

1. Construct a set of trees with root nodes that contain each of the individual

symbols and their weights.

2. Place the set of trees into a priority queue.

3. while the priority queue has more than one item

4. Remove the two trees with the smallest weights.

5. Combine them into a new binary tree in which the weight of the tree

root is the sum of the weights of its children.

6. Insert the newly created tree back into the priority queue.


Design the class

Designthe class


Implementation

Implementation

Listing 6.9-6.11 HuffmanTree, BuildTree and Decode Method


  • Login