CSS446 Spring 2014 Nan Wang. to study trees and binary trees to understand how binary search trees...

32
CSS446 Spring 2014 Nan Wang

Transcript of CSS446 Spring 2014 Nan Wang. to study trees and binary trees to understand how binary search trees...

Page 1: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

CSS446Spring 2014

Nan Wang

Page 2: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

to study trees and binary trees to understand how binary search trees can

implement sets to learn how red-black trees provide

performance guarantees for set operations to choose appropriate methods for tree

traversal to become familiar with the heap data

structure to use heaps for implementing priority

queues and for sorting

2

Page 3: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

In computer science, a tree is a hierarchical data structure composed of nodes.

Each node has a sequence of child nodes, and one of the nodes is the root node.

LinkedList -- linear chain of nodes Tree – a node can have more than one child

3

Page 4: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

4

Page 5: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

5

Page 6: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

A node holds ◦ a data item and ◦ a list of references to the child nodes

A tree holds a reference to the root node

6

It is different from It is different from the LinkedList nodethe LinkedList node

Page 7: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

When computing tree properties, it is common to recursively visit smaller and smaller subtrees.

7

1

23

Page 8: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

A binary tree consists of nodes, each of which has at most two child nodes.

8

Page 9: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

9

Page 10: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

10

Page 11: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

In a balanced tree, all paths from the root to the leaves have approximately the same length.

What is height of the tree? Which one hold more nodes?

11

Page 12: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

Given the height of a tree of h, what is the number of the nodes for a complete binary tree?

Given the number of nodes in a complete binary tree, what is the height of the tree?

12

Page 13: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

13

Page 14: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

Recursive Method

14

Recursive helperWithout recursive helperMethod in next slides

Page 15: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

15

Page 16: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

HashSet & TreeSet A set implementation rearrange its

elements in way so that finding elements quickly. (sort elements)

Binary Search takes O(log(n)) Array takes O(n)

16

Page 17: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

17

Page 18: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

18

Page 19: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

19

Page 20: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

20

Page 21: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

21

Page 22: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

22

Page 23: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

23

Page 24: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

24

Page 25: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

25

Page 26: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

26

Page 27: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

27

Page 28: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

28

Page 29: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

29

Page 30: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

30

Page 31: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

31

Start with an empty binary search tree.◦ Insert the keys 4,12,8,16,6,18,24,2,14,3, draw

the tree following each insert.◦ From the tree above, delete the keys,

6,14,16,4 in order, draw the search tree following each deletion.

Page 32: CSS446 Spring 2014 Nan Wang.  to study trees and binary trees  to understand how binary search trees can implement sets  to learn how red-black trees.

32