Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees Chapter 15.

Similar presentations


Presentation on theme: "Trees Chapter 15."— Presentation transcript:

1 Trees Chapter 15

2 Trees Lists, stacks, and queues are linear in their organization of data Items are one after another. In this chapter, we organize data in a nonlinear, hierarchical form Item can have more than one immediate successor

3 Terminology Use trees to represent relationships
Diagram represents the recursive calls for the fibonacci sequence as a tree Vertex or node Edges

4 FIGURE 15-1 A tree and one of its subtrees
Terminology Trees are hierarchical in nature Means a parent-child relationship between nodes FIGURE 15-1 A tree and one of its subtrees

5 Terminology More tree hierarchies

6 Terminology

7 Kinds of Trees General tree Binary tree Set T of one or more nodes
T is partitioned into disjoint subsets Binary tree Set of T nodes – either empty or partitioned into disjoint subsets Single node r, the root Two (possibly empty) sets – left and right subtrees

8 Kinds of Trees Binary trees that represent algebraic expressions – different tree traversal algorithms generate infix, prefix or postfix representations of the expression. Can we figure out the traversal strategies?

9 Kinds of Trees A binary search tree of names. What do we notice about the relationship of the values stored in the tree? Which tree traversal algorithm will give us the values in sorted order?

10 The Height of Trees Level of a node, n Height of a tree
If n is root, level 1 If n not the root, level is 1 greater than level of its parent Height of a tree Number of nodes on longest path from root to a leaf T empty, height 0 T not empty, height equal to max level of nodes

11 The Height of Trees Binary trees with the same number of nodes but different heights

12 Full, Complete, and Balanced Binary Trees
A full binary tree of height 3 – all parents have two (k) children and all leaves are at the same level.

13 Full, Complete, and Balanced Binary Trees
A complete binary tree (remember our Heap?) – tree is filled from left to right, only lower two levels of leaves differ in height: leaves at height h and height h-1.

14 The Maximum and Minimum Heights of a Binary Tree
Binary tree with n nodes Max height is n To minimize height of binary tree of n nodes Fill each level of tree as completely as possible A complete tree meets this requirement

15 The Maximum and Minimum Heights of a Binary Tree
Binary trees of height 3 A binary tree of height n will have a minimum of ____ nodes? And a maximum of _____ nodes? (Think about a power of 2?)

16 The Maximum and Minimum Number of Nodes in a Binary Tree
Maximum - Counting the nodes in a full binary tree of height h

17 The ADT Binary Tree Operations of ADT binary tree
Add, remove a node Set, retrieve data Test for empty Traversal operations that visit every node Traversal can visit nodes in several different orders

18 Traversals of a Binary Tree
Pseudo-code for general form of a recursive traversal algorithm (this is a preorder traversal – root, then left, then right).

19 Traversals of a Binary Tree
Options for when to visit the root Preorder: before it traverses both subtrees Inorder: after it traverses left subtree, before it traverses right subtree Postorder: after it traverses both subtrees Note traversal is O(n)

20 Traversals of a Binary Tree
Three traversals of a binary tree

21 Traversals of a Binary Tree
Preorder traversal algorithm

22 Traversals of a Binary Tree
Inorder traversal algorithm

23 Traversals of a Binary Tree
Postorder traversal algorithm

24 Binary Tree Operations
UML diagram for the class BinaryTree

25 Interface Template for the ADT Binary Tree
An interface template for the ADT binary tree

26 Interface Template for the ADT Binary Tree
An interface template for the ADT binary tree

27 Interface Template for the ADT Binary Tree
An interface template for the ADT binary tree

28 Interface Template for the ADT Binary Tree
An interface template for the ADT binary tree

29 The ADT Binary Search Tree
Recursive definition of a binary search tree n’s value is greater than all values in its left subtree TL. n’s value is less than all values in its right subtree TR. Both TL and TR are binary search trees.

30 The ADT Binary Search Tree
A binary search tree of names

31 Binary Search Tree Operations
Binary search trees with the same data as in previous figure – tree structure depends on order of node insertion.

32 Binary Search Tree Operations
Operations that define the ADT binary search tree

33 Searching a Binary Search Tree
Search algorithm for a binary search tree? Lets try to construct the algorithm….

34 Searching a Binary Search Tree – for Finn
Empty subtree where the search algorithm terminates when looking for Finn

35 Traversals of a Binary Search Tree
Inorder traversal of a binary search tree visits tree nodes in sorted search-key order

36 Efficiency of Binary Search Tree Operations
Max number of comparisons for retrieval, addition, or removal The height of the tree Adding entries in sorted order Produces maximum-height binary search tree Adding entries in random order Produces near-minimum-height binary search tree

37 Efficiency of Binary Search Tree Operations
The Big O for the retrieval, addition, removal, and traversal operations of the ADT binary search tree. Worst case occurs when tree is unbalanced and skewed to left or right.

38 End Chapter 15


Download ppt "Trees Chapter 15."

Similar presentations


Ads by Google