20
Lecture12: Tree II Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Lecture12: Tree II Bohyung Han CSE, POSTECH [email protected] CSED233: Data Structures (2014F)

Embed Size (px)

Citation preview

Page 1: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

Lecture12: Tree II

Bohyung HanCSE, POSTECH

[email protected]

CSED233: Data Structures (2014F)

Page 2: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

2 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Tree Traversal

• Process of visiting nodes in a tree systematically Some algorithms need to visit all nodes in a tree. Example: printing, counting nodes, etc.

• Implementation Can be done easily by recursion Order of visits does matter.

Computers”R”Us

Sales

R&D

Manufactur-ing

Lap-tops

Desk-tops

US

Interna-tional

Eu-rope

Asia

Canada

Page 3: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

3 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Preorder Traversal

• A preorder traversal of the subtree rooted at node n: Visit node n (process the node's data). Recursively perform a preorder traversal of the left child. Recursively perform a preorder traversal of the right child.

• A preorder traversal starts at the root.

public void preOrderTraversal(Node n){ if (n == null) return;

System.out.print(n.value+" "); preOrderTraversal(n.left); preOrderTraversal(n.right);}

Page 4: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

4 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Preorder Traversal

1

2

3 4

5 6

7

8

9

public void preOrderTraversal(Node n){ if (n == null) return;

System.out.print(n.value+" "); preOrderTraversal(n.left); preOrderTraversal(n.right);}

Page 5: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

5 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Inorder Traversal

• A preorder traversal of the subtree rooted at node n: Recursively perform an inorder traversal of the left child. Visit node n (process the node's data). Recursively perform an inorder traversal of the right child.

• An iorder traversal starts at the root.

public void inOrderTraversal(Node n){ if (n == null) return;

inOrderTraversal(n.left); System.out.print(n.value+" "); inOrderTraversal(n.right);}

Defined only in binary tree.

Page 6: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

6 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Inorder Traversal

6

2

1 4

3 5

7

9

8

public void inOrderTraversal(Node n){ if (n == null) return;

inOrderTraversal(n.left); System.out.print(n.value+" "); inOrderTraversal(n.right);}

Page 7: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

7 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Print Arithmetic Expressions

• Specialization of an inorder traversal print operand or operator when visiting node print “(“ before traversing left subtree print “)“ after traversing right subtree

Algorithm printExpression(v)if hasLeft (v)

print(“(’’)printExpression (left(v))

print(v.element ())if hasRight (v)

printExpression (right(v))print (“)’’)

+

÷

-2

a 1

3 b3

1

2

5

6

7 9

8

4

( (𝟐÷ (𝒂−𝟏 ) )+(𝟑×𝒃) )

(

(

( )

) ( )

)

Page 8: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

8 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Postorder Traversal

• A preorder traversal of the subtree rooted at node n: Recursively perform a postorder traversal of the left child. Recursively perform a postorder traversal of the right child. Visit node n (process the node's data).

• A postorder traversal starts at the root.

public void postOrderTraversal(Node n){ if (n == null) return;

postOrderTraversal(n.left); postOrderTraversal(n.right); System.out.print(n.value+" ");}

Page 9: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

9 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Postorder Traversal

9

5

1 4

2 3

8

7

6

public void postOrderTraversal(Node n){ if (n == null) return;

postOrderTraversal(n.left); postOrderTraversal(n.right); System.out.print(n.value+" ");}

Page 10: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

10 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Evaluate Arithmetic Expressions

• Specialization of a postorder traversal Recursive method returning the value of a subtree When visiting an internal node, combine the values of the subtrees

Algorithm evalExpr(v)if isExternal (v)

return v.element ()else

x evalExpr(leftChild (v))y evalExpr(rightChild

(v)) operator stored at v

return x y

+

-2

5 1

3 2

3

1

2

5

6 7

9

8

4

2 5 1 x 3 2 x +‐

Page 11: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

11 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Binary Search Trees

• A binary search tree is a binary tree storing keys at its nodesand satisfying the following property: Let u, v, and w be three nodes such that u is the left child v and w is

the right child of v. Then, we have key(u) key(v) key(w). The key value in v is larger than all keys in its left subtree and smaller

than all keys in its right subtree.

• An inorder traversal of a binary search trees visits the keys inan increasing order.

6

92

41 8

v

u w

Page 12: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

12 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Search

• Searching a key To search for a key k, we trace

a downward path starting at the root

The next node visited depends on the comparison of k withthe key of the current node

Search until we reach a leaf.

• Example: get(4) Call TreeSearch(4, root)

• The algorithms for floorEn-tryand ceilingEntry are simi-lar.

Algorithm TreeSearch(k, v)if T.isExternal (v)

return vif k < key(v)

return TreeSearch(k, T.left(v))else if k = key(v)

return velse

return TreeSearch(k, T.right(v))

6

92

41 8

<

>

=

Page 13: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

13 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion

• Inserting a node To perform operation insert(k), we search for key k using TreeSearch

Assume k is not already in the tree, and let w be the leaf reached by the search

We insert k at node w and expand w into an internal node

• Example: insert 5

6

92

41 8

<

>

>

w

6

92

41 8

5w

Page 14: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

14 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Insertion

public void insert(int i){ root = recursiveInsert(root, i);}

private Node recursiveInsert(Node n, int i){ if (n == null) return new Node(i);

if (i < n.value) { // insert in the left subtree n.left = recursiveInsert(n.left,i); return n; } else { // insert in the right subtree n.right = recursiveInsert(n.right,i); return n; }}

Page 15: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

15 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Deletion

• Deleting a node with a child To perform operation remove(k), we search for key k

Assume key k is in the tree, and let v be the node storing k

Simply connect parent and child of v.

• Example: remove 4

6

92

41 8

5

vw

6

92

51 8

<

>

Page 16: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

16 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Deletion (cont.)

• Deleting a node with two children We consider the case where

the key k to be removed is stored at a node v whose children are both internal.

We find the internal node w that follows v in an inorder traversal.

we copy key(w) into node v. we remove node w.

• Example: remove 3

3

1

8

6 9

5

v

w

z

2

5

1

8

6 9

v

2

Page 17: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

17 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Implementation of Deletion

public void delete(int i){ root = recursiveRemove(root,i);}

private Node recursiveRemove(Node n, int i){ if (n == null) // end of tree (node not found) return null;

if (i < n.value) { // recurse left n.left = recursiveRemove(n.left,i); return n; } else if (i > n.value) { // recurse right n.right = recursiveRemove(n.right,i); return n; }

Page 18: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

18 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Implementation of Deletion else { // match if (n.left == null && n.right == null) { // no child return null; } else if (n.left != null && n.right == null) { // left child only return n.left; } else if (n.left == null && n.right != null) { // right child only return n.right; } else { // two children Node maxLeft = findMax(n.left); // find node to replace recursiveRemove(n.left, maxLeft.value); // remove the node maxLeft.left = n.left; // set children of replacement node maxLeft.right = n.right; return maxLeft; // return replacement node (adds it to tree) } }}

Page 19: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

19 CSED233: Data Structuresby Prof. Bohyung Han, Fall 2014

Performance

• Consider ordered set items implemented by means of a binary search tree of height The space used is . Methods search, insert

and delete take time.

• The height in the worst case. in the best case.

• We want a balanced binary tree!

Page 20: Lecture12: Tree II Bohyung Han CSE, POSTECH bhhan@postech.ac.kr CSED233: Data Structures (2014F)

20