Trees Tiziana Ligorio 1 Todays Plan Trees Binary Tree ADT - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

Trees Tiziana Ligorio 1 Todays Plan Trees Binary Tree ADT - - PowerPoint PPT Presentation

Trees Tiziana Ligorio 1 Todays Plan Trees Binary Tree ADT Binary Search Tree ADT 2 Announcements Questions? 3 ADT Operations we have seen so far Bag, List, Stack, Queue Add data to collection Remove data from


slide-1
SLIDE 1

Trees

Tiziana Ligorio

1

slide-2
SLIDE 2

Today’s Plan

Trees Binary Tree ADT Binary Search Tree ADT

2

slide-3
SLIDE 3

Announcements

Questions?

3

slide-4
SLIDE 4

ADT Operations we have seen so far

Bag, List, Stack, Queue Add data to collection
 Remove data from collection
 Retrieve data from collection
 
 Stack and Queue always position based Bag, retrieval always value based (there are no positions) List has both. For all of them, data organization is linear

4

slide-5
SLIDE 5

5

slide-6
SLIDE 6

Tree

Non-linear structure A special type of graph Can represent relationships Hierarchical (directional) organization (E.g. family tree)

6

slide-7
SLIDE 7

7

What can you tell me about this tree?

slide-8
SLIDE 8

8

Node Edge

slide-9
SLIDE 9

9

Node Child Parent Siblings Ancestor Descendant Edge

slide-10
SLIDE 10

10

Node Child Parent Siblings Leaf Root Ancestor Descendant Edge Subtree

slide-11
SLIDE 11

11

Node Child Parent Siblings Leaf Root Ancestor Descendant Edge Subtree How many leaves in this tree?

slide-12
SLIDE 12

12

Node Child Parent Siblings Leaf Root Ancestor Descendant Edge Subtree Path Height = 4

slide-13
SLIDE 13

Path: a sequence of nodes c1, c2, ..., ck where ci+1 is a child of ci. Height: the number of nodes in the longest path from the root to a leaf. Subtree: the subtree rooted at node n is the tree formed by taking n as the root node and including all its descendants.

13

slide-14
SLIDE 14

Different shapes/structures

14

Both n = 16 Both 11 leaves Different height

slide-15
SLIDE 15

We have already seen Trees!

Mostly as a “thinking tool”

  • Decision Trees
  • Divide and Conquer

15

slide-16
SLIDE 16

Binary Tree ADT

16

slide-17
SLIDE 17

BinaryTree

17

Each node has at most 2 children

slide-18
SLIDE 18

BinaryTree

18

Each node has at most 2 children Left Child Right Child Left Subtree Right Subtree

slide-19
SLIDE 19

Different shapes/structures

19

Both h = 3 and one leaf But different

slide-20
SLIDE 20

Binary Tree Applications

20

slide-21
SLIDE 21

Algebraic Expressions

21

* + + 3 * 3 4

(3 + 4) * 5

5

3 + 4 * 5

4 5

slide-22
SLIDE 22

Decision Tree

22

Is it a mammal?

Does it have stripes? Does it fly? Is it carnivore? It’s a pig It’s an eagle It’s an ostrich It’s a tiger It’s a zebra Yes Yes Yes Yes No No No No

slide-23
SLIDE 23

Huffman Tree

Huffman Encoding Compression Algorithm (Huffman Encoding):
 “In 1951, David A. Huffman for his MIT Information Theory class term paper hit upon the idea of using a frequency-sorted binary tree and quickly proved this method the most efficient.” IDEA: Encode symbols into a sequence of bits s.t. most frequent symbols have shortest encoding Not encryption but compression => use shortest code for most frequent symbols No codeword is prefix to another codeword (i.e. if a symbol is encoded as 00 no other codeword can start with 00)

23

slide-24
SLIDE 24

Huffman Tree

24

50% 20% 20% 5% 3% 2%

slide-25
SLIDE 25

Huffman Tree

25

50% 20% 20% 5% 3% 2% 5%

slide-26
SLIDE 26

Huffman Tree

26

50% 20% 20% 3% 2% 5% 10% 5%

slide-27
SLIDE 27

Huffman Tree

27

50% 20% 3% 2% 5% 10% 5% 30% 20%

slide-28
SLIDE 28

Huffman Tree

28

50% 20% 3% 2% 5% 10% 5% 30% 20% 50%

slide-29
SLIDE 29

Huffman Tree

29

50% 20% 3% 2% 5% 10% 5% 30% 20% 50% 100%

slide-30
SLIDE 30

Huffman Tree

30

50% 20% 3% 2% 5% 10% 5% 30% 20% 50% 100%

1 1 1 1 1

slide-31
SLIDE 31

Huffman Tree

31

50% 20% 3% 2% 5% 10% 5% 30% 20% 50% 100%

1 1 1 1 1

100 11 1010 10110 10111

slide-32
SLIDE 32

Lecture Activity

Think about structure! Draw ALL POSSIBLE binary trees with 4 nodes Label each tree with its height and number of leaves.

32

slide-33
SLIDE 33

33

IMGAGE FROM : http://www.durangobill.com/BinTrees.html

slide-34
SLIDE 34

Tree Structure

34

A C D E F G B C D E G A E B D C F G E B D C F G B A F A

h = 3 h = 5 h = 7 h = 7

slide-35
SLIDE 35

Structure definitions may vary across different sources. The following comes form your textbook and will be used in this course and on exams

35

slide-36
SLIDE 36

Tree Structure

36

A C D E F G B C D E G A E B D C F G E B D C F G B A F A

h = 3 h = 5 h = 7 h = 7 What is the maximum (minimum) height of a tree with 7 nodes?

slide-37
SLIDE 37

Tree Structure

37

A C D E F G B C D E G A E B D C F G E B D C F G B A F A

h = 3 h = 5 h = 7 h = 7 WE WILL LOOK AT THE GENERAL ANSWER NEXT

slide-38
SLIDE 38

Full Binary Tree

38

Every node that is not a leaf has exactly 2 children Every node has left and right subtrees of same height All leaves are at same level h

slide-39
SLIDE 39

Complete Binary Tree

39

A three that is full up to level h-1, with level h filled in from left to right All nodes at levels h-2 and above have exactly 2 children When a node at level h-1 has children, all nodes to its left have exactly 2 children When a node at level h-1 has

  • ne child, it is a left child
slide-40
SLIDE 40

(Height) Balanced Binary Tree

40

For any node, its left and right subtrees differ in height by no more than 1 All paths from root to leaf differ in length by at most 1

slide-41
SLIDE 41

41

Balanced Unbalanced

slide-42
SLIDE 42

Maximum Height

42

n nodes every node 1 child

h = n

Essentially a chain

slide-43
SLIDE 43

Minimum Height

43

Binary tree of height h can have up to n = 2h - 1 For example for h = 3, 1 + 2 + 4 = 7 = 23 - 1 h = log2 (n+1) for a full binary tree For example: 1,000 nodes h ≈ 10 (1,000 ≈ 210) 1,000,000 nodes h ≈ 20 (106 ≈ 220)

slide-44
SLIDE 44

Minimum Height

44

Binary tree of height h can have up to n = 2h - 1 For example for h = 3, 1 + 2 + 4 = 7 = 23 - 1 h = log2 (n+1) for a full binary tree For example: 1,000 nodes h ≈ 10 (1,000 ≈ 210) 1,000,000 nodes h ≈ 20 (106 ≈ 220)

Important when we will be looking for things in trees given some order!!! Recall analysis of Divide and Conquer algorithms

slide-45
SLIDE 45

45

. . .

1 = 21 -1 1 h Total n 3 = 22 -1 2 7 = 23 -1 3 15 = 24 -1 4 n @ level 1 = 20 2 = 21 4 = 22 8 = 23 2h -1 h 2h-1

In a full tree:

slide-46
SLIDE 46

Binary Tree Traversals

46

slide-47
SLIDE 47

47

r TR TL Visit (retrieve, print, modify …) every node in the tree Essentially visit the root as well as it’s subtrees 
 Order matters!!!

slide-48
SLIDE 48

48

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder:

slide-49
SLIDE 49

49

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60

slide-50
SLIDE 50

50

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20

slide-51
SLIDE 51

51

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10

slide-52
SLIDE 52

52

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10

slide-53
SLIDE 53

53

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10

slide-54
SLIDE 54

54

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40

slide-55
SLIDE 55

55

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30

slide-56
SLIDE 56

56

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30

slide-57
SLIDE 57

57

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30

slide-58
SLIDE 58

58

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50

slide-59
SLIDE 59

59

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50

slide-60
SLIDE 60

60

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50

slide-61
SLIDE 61

61

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50, 70

slide-62
SLIDE 62

62

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50, 70

slide-63
SLIDE 63

63

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Preorder Traversal: if (T is not empty) //implicit base case
 {
 visit the root r
 traverse TL
 traverse TR
 }
 10 40 50 30 70 20 60

Preorder: 60, 20, 10, 40, 30, 50, 70

slide-64
SLIDE 64

64

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Inorder Traversal: if (T is not empty) //implicit base case
 {
 traverse TL
 visit the root r
 traverse TR
 }
 10 40 50 30 70 20 60

Inorder: 10, 20, 30, 40, 50, 60, 70

slide-65
SLIDE 65

65

r TR TL Visit (retrieve, print, modify …) every node in the tree
 Postorder Traversal: if (T is not empty) //implicit base case
 {
 traverse TL
 traverse TR 
 visit the root r
 }
 10 40 50 30 70 20 60

Postorder: 10, 30, 50, 40, 20, 70, 60

slide-66
SLIDE 66

BinaryTree ADT Operations

66

? ? ? ? ? ? ? ? ? ? ? ? ?

slide-67
SLIDE 67

67

#ifndef BinaryTree_H_
 #define BinaryTree_H_
 template<typename ItemType>
 class BinaryTree
 {
 public:
 BinaryTree(); // constructor
 BinaryTree(const BinaryTree<ItemType>& tree); // copy constructor
 ~BinaryTree(); // destructor
 bool isEmpty() const;
 size_t getHeight() const;
 size_t getNumberOfNodes() const;
 void add(const ItemType& new_item);
 void remove(const ItemType& new_item);
 ItemType find(const ItemType& item) const;
 void clear();
 void preorderTraverse(Visitor<ItemType>& visit) const;
 void inorderTraverse(Visitor<ItemType>& visit) const;
 void postorderTraverse(Visitor<ItemType>& visit) const;
 BinaryTree& operator= (const BinaryTree<ItemType>& rhs);
 private: // implementation details here
 };// end BST #include "BinaryTree.cpp"
 #endif // BinaryTree_H_

slide-68
SLIDE 68

#ifndef BinaryTree_H_
 #define BinaryTree_H_
 template<typename ItemType>
 class BinaryTree
 {
 public:
 BinaryTree(); // constructor
 BinaryTree(const BinaryTree<ItemType>& tree); // copy constructor
 ~BinaryTree(); // destructor
 bool isEmpty() const;
 size_t getHeight() const;
 size_t getNumberOfNodes() const;
 void add(const ItemType& new_item);
 void remove(const ItemType& new_item);
 ItemType find(const ItemType& item) const;
 void clear();
 void preorderTraverse(Visitor<ItemType>& visit) const;
 void inorderTraverse(Visitor<ItemType>& visit) const;
 void postorderTraverse(Visitor<ItemType>& visit) const;
 BinaryTree& operator= (const BinaryTree<ItemType>& rhs);
 private: // implementation details here
 };// end BST #include "BinaryTree.cpp"
 #endif // BinaryTree_H_

68

How might you add Will determine the tree structure This is an abstract class from which we can derive desired behavior keeping the traversal general

slide-69
SLIDE 69

Considerations

69

slide-70
SLIDE 70

Recall

Remember our Bag ADT?


  • Array implementation

  • Linked Chain implementation

  • Assume no duplicates

Find an element: O(n) Remove: Find element and if there remove it O(n) Add: Check if element is there and if not add it O(n)

70

slide-71
SLIDE 71

Recall

Remember our Bag ADT?


  • Array implementation

  • Linked Chain implementation

  • Assume no duplicates

Find an element: O(n) Remove: Find element and if there remove it O(n) Add: Check if element is there and if not add it O(n)

71

Can we do better?

slide-72
SLIDE 72

A Different Approach

72

  • 2

2

  • 1

3 6 4

slide-73
SLIDE 73

A Different Approach

73

  • 2

2

  • 1

3 6 4

slide-74
SLIDE 74

A Different Approach

74

  • 2
  • 1

3 6 4 2

slide-75
SLIDE 75

A Different Approach

75

  • 2
  • 1

3 6 4 2

slide-76
SLIDE 76

A Different Approach

76

  • 2
  • 1

3 6 4 2

slide-77
SLIDE 77

A Different Approach

77

  • 2
  • 1

3 6 4 2

slide-78
SLIDE 78

A Different Approach

78

  • 2

3 6 4 2

  • 1
slide-79
SLIDE 79

A Different Approach

79

  • 2

3 6 4 2

  • 1
slide-80
SLIDE 80

A Different Approach

80

  • 2

3 6 4 2

  • 1
slide-81
SLIDE 81

A Different Approach

81

  • 2

3 6 4 2

  • 1
slide-82
SLIDE 82

A Different Approach

82

  • 2

6 3 2

  • 1

4

slide-83
SLIDE 83

A Different Approach

83

  • 2

6 3 2

  • 1

4

slide-84
SLIDE 84

A Different Approach

84

  • 2

6 3 2

  • 1

4

slide-85
SLIDE 85

A Different Approach

85

  • 2

6 3 2

  • 1

4

Find 5

slide-86
SLIDE 86

A Different Approach

86

  • 2

6 3 2

  • 1

4

Find 5

slide-87
SLIDE 87

A Different Approach

87

  • 2

6 3 2

  • 1

4

Find 5

slide-88
SLIDE 88

A Different Approach

88

  • 2

6 3 2

  • 1

4

Find 5

slide-89
SLIDE 89

A Different Approach

89

  • 2

6 3 2

  • 1

4 What’s special about the shape of this tree?

slide-90
SLIDE 90

Binary Search Tree

90

  • 2

6 3 2

  • 1

4 Structural Property: For each node n
 n > all values in TL n < all values in TR

slide-91
SLIDE 91

BST Formally

Let S be a set of values upon which a total ordering relation <, is defined. For example, S can be the set of integers. 
 A binary search tree (BST) T for the ordered set (S,<) is a binary tree with the following properties:

  • Each node of T has a value. If p and q are nodes, then we write p < q to mean that

the value of p is less than the value of q. 


  • For each node n ∈ T, if p is a node in the left subtree of n, then p < n. 

  • For each node n ∈ T, if p is a node in the right subtree of n, then n < p. 

  • For each element s ∈ S there exists a node n ∈ T such that s = n.

91

slide-92
SLIDE 92

Binary Search Tree

92

Structural Property: For each node n
 n > all values in TL n < all values in TR r TR
 > r TL < r search(bs_tree, item)
 { if (bs_tree is empty) //base case
 item not found
 else if (item == root)
 return root
 else if (item < root)
 search(TL , item)
 else // item > root
 search(TR , item)
 }


slide-93
SLIDE 93

Inserting into a BST

93

  • 2

6 3 2

  • 1

4 1

slide-94
SLIDE 94

Inserting into a BST

94

  • 2

6 3 2

  • 1

4 1

slide-95
SLIDE 95

Inserting into a BST

95

  • 2

6 3 2

  • 1

4 1

slide-96
SLIDE 96

Inserting into a BST

96

  • 2

6 3 2

  • 1

4 1

slide-97
SLIDE 97

Inserting into a BST

97

  • 2

6 3 2

  • 1

4 1

slide-98
SLIDE 98

Inserting into a BST

98

  • 2

6 3 2

  • 1

4 1

slide-99
SLIDE 99

Inserting into a BST

99

  • 2

6 3 2

  • 1

4 1 5

slide-100
SLIDE 100

Inserting into a BST

100

  • 2

6 3 2

  • 1

4 1 5

slide-101
SLIDE 101

Inserting into a BST

101

  • 2

6 3 2

  • 1

4 1 5

slide-102
SLIDE 102

Inserting into a BST

102

  • 2

6 3 2

  • 1

4 1 5

slide-103
SLIDE 103

Inserting into a BST

103

  • 2

6 3 2

  • 1

4 1 5

slide-104
SLIDE 104

Inserting into a BST

104

  • 2

6 3 2

  • 1

4 1 5 You Grow a tree with BST property, you don’t get to restructure it (Self-balancing trees (e.g.Red-Black trees) will do that, perhaps in CSCI 335)

slide-105
SLIDE 105

Growing a BST

105

7 4 10 2 1

slide-106
SLIDE 106

Growing a BST

106

7 4 10 2 1 1 6 3 2 4 5

slide-107
SLIDE 107

Lecture Activity

Write pseudocode to insert an item into a BST

107

slide-108
SLIDE 108

Inserting into a BST

108

r TR
 > r TL < r add(bs_tree, item)
 { if (bs_tree is empty) //base case
 make item the root
 else if (item < root)
 add(TL , item)
 else // item > root
 add(TR , item)
 }


slide-109
SLIDE 109

Traversing a BST

109

r TR
 > r TL < r Same as traversing any binary tree Which type of traversal is special for a BST?

slide-110
SLIDE 110

Traversing a BST

110

r TR
 > r TL < r inorder(bs_tree)
 {
 //implicit base case if (bs_tree is not empty)
 {
 inorder(TL)
 visit the root
 inorder(TR)
 }
 }
 Same as traversing any binary tree Visits nodes in sorted ascending order

slide-111
SLIDE 111

Efficiency of BST

Searching is key to most operations Think about the structure and height of the tree

111

slide-112
SLIDE 112

Efficiency of BST

Searching is key to most operations Think about the structure and height of the tree O(h) What is the maximum height? What is the minimum height?

112

slide-113
SLIDE 113

Tree Structure

113

5 8 1 4 7 9 7 3 5 4 8 9 3 1

h = 3 h = 7 n nodes

log2 (n+1) <= h <= n

Full BST Chain

n = 7

slide-114
SLIDE 114

114

Operation In Full Tree Worst-case Search log2(n+1) 휪(h) Add log2(n+1) 휪(h) Remove log2(n+1) 휪(h) Traverse n 휪(n)

slide-115
SLIDE 115

BST Operations

115

? ? ? ? ? ? ? ? ? ? ? ? ?

slide-116
SLIDE 116

116

#ifndef BST_H_
 #define BST_H_
 template<typename ItemType>
 class BST
 {
 public:
 BST(); // constructor
 BST(const BST<ItemType>& tree); // copy constructor
 ~ BST(); // destructor
 bool isEmpty() const;
 size_t getHeight() const;
 size_t getNumberOfNodes() const;
 void add(const ItemType& new_item);
 void remove(const ItemType& new_item);
 ItemType find(const ItemType& item) const;
 void clear();
 void preorderTraverse(Visitor<ItemType>& visit) const;
 void inorderTraverse(Visitor<ItemType>& visit) const;
 void postorderTraverse(Visitor<ItemType>& visit) const;
 BST& operator= (const BST<ItemType>& rhs);
 private: //implementation details here 
 };//end BST #include "BST.cpp"
 #endif // BST_H_

Looks a lot like a BinaryTree Might you inherit from it? What would you

  • verride?

This is an abstract class from which we can derive desired behavior keeping the traversal general

slide-117
SLIDE 117

#ifndef BST_H_
 #define BST_H_
 template<typename ItemType>
 class BST
 {
 public:
 BST(); // constructor
 BST(const BST<ItemType>& tree); // copy constructor
 ~ BST(); // destructor
 bool isEmpty() const;
 size_t getHeight() const;
 size_t getNumberOfNodes() const;
 void add(const ItemType& new_item);
 void remove(const ItemType& new_item);
 ItemType find(const ItemType& item) const;
 void clear();
 void preorderTraverse(Visitor<ItemType>& visit) const;
 void inorderTraverse(Visitor<ItemType>& visit) const;
 void postorderTraverse(Visitor<ItemType>& visit) const;
 BST& operator= (const BST<ItemType>& rhs);
 private: //implementation details here
 };//end BST #include "BST.cpp"
 #endif // BST_H_

117

Looks a lot like a BinaryTree Might you inherit from it? What would you

  • verride?

This is an abstract class from which we can derive desired behavior keeping the traversal general