1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Java methods: Object-oriented programming and data structures (2nd AP edition): Chapter 24 - Maria Litvin, Gary Litvin

30 34 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 375,55 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 24 - Binary trees. In this chapter, the learning objectives are: Learn about binary trees; learn how to represent and handle a binary tree using the TreeNode class; learn about binary search trees; review sets and maps, and the java.util classes that implement them.

Trang 1

and Data Structures

Maria Litvin ● Gary Litvin

2nd AP edition with GridWorld

Trang 2

Objectives:

classes that implement them

Trang 3

Some Applications of Trees

Trang 4

Number of levels (equals

5 here)

node

node’s

right subtree

Trang 5

Binary Tree Properties

example, a binary tree with 20 levels can

nodes.

where to proceed, left or right (used in binary search trees).

compared to the total number of nodes.

Trang 6

The TreeNode Class

instead of next has left and right

public class TreeNode

{

Holds a reference to

the left childHolds a reference to the right child

Trang 7

The TreeNode Class (cont’d)

// Constructors:

public TreeNode (Object v)

{ value = v; left = null; right = null; }

public TreeNode (Object v, TreeNode lt, TreeNode rt)

{ value = v; left = lt; right = rt; }

// Methods:

}

Trang 8

Trees and Recursion

the left and right subtrees are smaller trees:

{ // Base case: root == null, // the tree is empty do nothing

if (root != null) // Recursive case {

process (root.getValue ( ));

traverse (root.getLeft ( ));

traverse (root.getRight ( ));

} }

Trang 10

TreeNode Example 2

// returns a reference to a new tree, which is a

// copy of the tree rooted at root

Trang 11

traverse the left and right subtrees.

A/ \

B C / \

D E

A B D E C

Trang 12

process the root, then traverse the right

subtree.

A/ \

B C / \

D E

D B E A C

Trang 13

subtrees, then process the root.

A/ \

B C / \

D E

D E B C A

Trang 14

Traversals (cont’d)

Trang 15

Binary Search Trees (BST)

comparator is supplied).

subtree are smaller than the value in the node and all the values in its right subtree are

larger than the value in the node.

15 / \

8 20 / \

1 12

15 / \

12 20

/ \

1 8

Trang 16

BST (cont’d)

for quick searching and linked lists for

inserting and deleting values.

Trang 17

// root refers to a BST; the nodes hold Strings

Trang 18

{

TreeNode node = root;

while ( node != null )

Trang 22

Find the smallest

node of the right

Link that note in

place of the root and

remove the old root

Trang 23

BST (cont’d)

remove , and contains methods take O(log n)

time, where n is the number of nodes in the

tree.

degenerate into a nearly linear shape

tree balanced.

Trang 24

java.util.TreeSet<E>

• compareTo (or comparator’s compare ) is

methods.

implements inorder traversal.

in ascending order.

Trang 25

java.util.TreeSet<E> (cont’d)

Never mind

Trang 26

java.util.TreeMap<K,V>

• compareTo (or comparator’s compare ) for

remove methods.

• The keySet method returns a TreeSet<K>

Trang 27

java.util.TreeMap<K,V> (cont’d)

Trang 28

Review:

for data retrieval applications?

Trang 29

Review (cont’d):

in a binary tree with 10 levels?

a node in a doubly-linked list?

and postorder traversals of the following tree?

A / \

B C / \

D E

Trang 30

Review (cont’d):

property What is it?

2 3 / \

4 5

4 / \

2 5 / \

1 3

Ngày đăng: 04/11/2020, 23:19