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

Preview:

Citation preview

CSS446Spring 2014

Nan Wang

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

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

4

5

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

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

7

1

23

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

8

9

10

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

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

13

Recursive Method

14

Recursive helperWithout recursive helperMethod in next slides

15

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

17

18

19

20

21

22

23

24

25

26

27

28

29

30

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.

32

Recommended