Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node.

Similar presentations


Presentation on theme: "Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node."— Presentation transcript:

1 Chapter 6 – Trees

2 Notice that in a tree, there is exactly one path from the root to each node

3 Trees – linked lists, stacks, and queues which are linear data structures – A tree is a nonlinear data structure: a tree is a collection of nodes connected by edges root at the top the leaves (i.e., terminal nodes) at the bottom The root has no parent; Leaves, on the other hand, have no children, or rather, their children are empty structures

4 A Recursive Definition 1. An empty structure is a tree (a tree is a collection of nodes and the collection can be empty) 2. If T 1, …, T k are trees, then the structure whose root has as its children the roots of T 1, …, T k is also a tree 3. Only structures generated by rules 1 and 2 are trees

5 A Recursive Definition (cont’d) ROOT OF TREE T T1T1 T2T2 T3T3 TkTk SUBTREES

6 An Example A BC DE F G H I N nodes, N-1 edges in a tree.

7 Some Definitions Nodes with no children are leaves: (C,E,F,H,I), they are also called external nodes. Nodes which are not leaves are called internal nodes Nodes with the same parents are siblings: (B,C,D,E) and (G,H) A path from node n i to node n j is the sequence of directed edges from n i to n j The level or depth of a node n i is the number of edges from the root to n i. The depth of the root is 0

8 Some Definitions (cont’d) The height of a node n i is the length of the longest path from n i to a leaf. The height of a leaf node is 0 The height of a tree is equal to the height of the root

9 Binary Trees – An Informal Definition A binary tree is a tree in which no node can have more than two children Each node has 0, 1, or 2 children

10 Binary Trees – A Recursive Definition 1. An empty structure is a binary tree 2. If T 1 and T 2 are binary trees, then the structure whose root has as its children the roots of T 1 and T 2 is also a binary tree 3. Only structures generated by rules 1 and 2 are binary trees

11 Trees vs. Binary Trees No node in a binary tree may have more than 2 children, whereas there is no limit on the number of children of a node in a tree


Download ppt "Chapter 6 – Trees. Notice that in a tree, there is exactly one path from the root to each node."

Similar presentations


Ads by Google