Trees Chapter 4 1 Objectives Understand the terminology of the - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

Trees Chapter 4 1 Objectives Understand the terminology of the - - PowerPoint PPT Presentation

Trees Chapter 4 1 Objectives Understand the terminology of the tree data structure Represent a tree structure in a program Understand the importance of the binary trees Use a binary search tree for storing ordered elements 2 Motivation


slide-1
SLIDE 1

Trees

Chapter 4

1

slide-2
SLIDE 2

Objectives

Understand the terminology of the tree data structure Represent a tree structure in a program Understand the importance of the binary trees Use a binary search tree for storing ordered elements

2

slide-3
SLIDE 3

Motivation

Why lists, stacks, and queues are not enough? Not everything can be linearized. We may need to represent hierarchies, for example. Sorted array search: O(log(n)) Sorted array insert: O(n) Linked list search: O(n) Linked list insert: O(1) Can we build a data structure that is fast for both search and insert?

3

slide-4
SLIDE 4

Hierarchical Structures

UC System UCR BCOE CSE ECE CNAS … UCI UCSD …

4

slide-5
SLIDE 5

Hierarchical Structures

US CA Riverside County Riverside Palm Springs San Bernardino County … AZ MN

5

slide-6
SLIDE 6

Definition

A tree can be defined recursively A tree is a group of nodes Each node contains a value If the tree is not empty, one node is identified as the root node The root node has zero or more subtrees The root of a subtree is connected to the root

  • f the tree

6

slide-7
SLIDE 7

Terminology: Basic Definitions

7

A B C D E I J F G H K Root Subtrees A is the parent of D D is the child of A B, C, and D are siblings E and F are not siblings

slide-8
SLIDE 8

Terminology: Descendants

8

A B C D E I J F G H K Descendants of A

slide-9
SLIDE 9

Terminology: Ancestors

9

A B C D E I J F G H K Ancestors of E Descendant of E

slide-10
SLIDE 10

Terminology: Leaves

10

A B C D E I J F G H K Leaf nodes (Leaves) Internal nodes

slide-11
SLIDE 11

Terminology: Levels, Depth

11

A B C D E I J F G H K Level 0 Level 1 Level 2 Level 3 J is at level 3 The depth of J is 3 What is the relationship between the depth of a node and the number of ancestors? What is the height of the tree?

slide-12
SLIDE 12

Terminology: Path

12

A B C D E I J F G H K The path from A to J is (A, B, E, J) The length of the path is three (edges) What is the path from D to K? Is there a path from B to C?

slide-13
SLIDE 13

Tree Representation

13

Node Value (any type) Children * * * * * template <type T> class Tree { class Node { T value; list<Node*> children; }; Node* root; };

slide-14
SLIDE 14

Parent Representation

14

A B C D E I J F G H K template <type T> class Tree { class Node { T value; Node* parent; }; list<Node*> nodes; };

slide-15
SLIDE 15

Left-child Right-sibling

15

A B C D E I J F G H K

slide-16
SLIDE 16

Left-child Right-sibling

16

A B C D E I J F G H K template <type T> class Tree { class Node { T value; Node* left_child; Node* right_sibling; }; Node* root; };

slide-17
SLIDE 17

Binary Trees

A special case where every node has at most two children Has many applications that make it particularly interesting More restricted  Room for optimization

17

template <type T> class Tree { class Node { T value; Node* left; Node* right; }; Node* root; };

slide-18
SLIDE 18

Application: Expression Tree

18

⨉ 3 5 / 4 2 ⨉ 2 +

3 × 5 + 4/2 × 2

slide-19
SLIDE 19

Inorder Tree Traversal

19

⨉ 3 5 / 4 2 ⨉ 2 +

(3 × 5) + (4/2 ) × 2

slide-20
SLIDE 20

Postorder Tree Traversal

20

⨉ 3 5 / 4 2 ⨉ 2 +

35 × 42/+2 ×

slide-21
SLIDE 21

Preorder Tree Traversal

21

⨉ 3 5 / 4 2 ⨉ 2 +

×+×35/422

slide-22
SLIDE 22

Implementation of Traversals

22

preorder(Node* root) { if (root == null) return; print(root->value); preorder(root->left); preorder(root->right); } postorder(Node* root) { if (root == null) return; postorder(root->left); postorder(root->right); print(root->value); } inorder(Node* root) { if (root == null) return; inorder(root->left); print(root->value); inorder(root->right); }