Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 112 Fundamentals of Programming II Introduction to Trees.

Similar presentations


Presentation on theme: "Computer Science 112 Fundamentals of Programming II Introduction to Trees."— Presentation transcript:

1 Computer Science 112 Fundamentals of Programming II Introduction to Trees

2 What Is a Tree? Each item has at most one predecessor Each item can have many successors

3 What Is a Tree? Start with a single node, called the root Root node

4 What Is a Tree? Start with a single node, called the root Each successor node is a child or daughter node Root node Daughters of root node

5 What Is a Tree? Successors are also called descendants Root node Descendants of root node

6 What Is a Tree? Nodes without successors are called leaf nodes The set of leaf nodes is the frontier Root node Leaf nodes (the frontier)

7 What Is a Tree? Nodes with at least one successor are called interior nodes Root node Interior nodes

8 What Is a Tree? X Predecessors are also called ancestors The immediate predecessor is called the parent Root node Ancestors of node X

9 What Is a Tree? Nodes with the same parent are called siblings Root node Siblings

10 What Is a Tree? Levels in a tree are numbered from 0 Root node Level 0 Level 1 Level 2 Level 3 There are three nodes in level 3

11 What Is a Tree? A binary tree allows at most two successors per node Root node

12 What Is a Tree? A general tree allows any number of successors per node Root node

13 Trees as Recursive Data Structures A tree is either –empty, or –consists of a node containing a datum one or more subtrees Each subtree is itself another tree

14 Tree Traversals We’d like to visit each data item in a tree Are the items randomly ordered, as in a bag or set? Think of visiting the data in a node, and its left and right subtrees, in some order

15 Order of nodes visited: D B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

16 Order of nodes visited: D B B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

17 Order of nodes visited: D B A B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

18 Order of nodes visited: D B A C B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

19 Order of nodes visited: D B A C F B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

20 Order of nodes visited: D B A C F E B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

21 Order of nodes visited: D B A C F E G B C F AE D G Preorder Traversal Visit the data Visit the left subtree Visit the right subtree

22 Order of nodes visited: A B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

23 Order of nodes visited: A B B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

24 Order of nodes visited: A B C B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

25 Order of nodes visited: A B C D B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

26 Order of nodes visited: A B C D E B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

27 Order of nodes visited: A B C D E F B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

28 Order of nodes visited: A B C D E F G B C F AE D G Inorder Traversal Visit the left subtree Visit the data Visit the right subtree

29 Order of nodes visited: A B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

30 Order of nodes visited: A C B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

31 Order of nodes visited: A C B B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

32 Order of nodes visited: A C B E B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

33 Order of nodes visited: A C B E G B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

34 Order of nodes visited: A C B E G F B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

35 Order of nodes visited: A C B E G F D B C F AE D G Postorder Traversal Visit the left subtree Visit the right subtree Visit the data

36 Order of nodes visited: D B F A C E G B C F AE D G Level Order Traversal For each level Visit data from left to right

37 Tree Applications File directories Processing sentences (computer programs or natural languages) Searchable data structures Heaps (implement heap sort, priority queues)

38 For Wednesday Binary Search Trees


Download ppt "Computer Science 112 Fundamentals of Programming II Introduction to Trees."

Similar presentations


Ads by Google