Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Similar presentations


Presentation on theme: "Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."— Presentation transcript:

1 Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

2 Overview 10.1 10.2 Introduces tree terminology
Discusses the implementation of binary trees 10.2 Traversing trees

3 Binary Trees

4 Binary Trees

5 Binary Trees

6 Binary Trees Binary tree Empty or,
A node with a left and right subtree. Each of these subtrees is itself a binary tree. Children The nodes directly below a node In a binary tree, no node can have more than two children.

7 Binary Trees Parent Siblings Root
Every node in a binary tree has exactly once parent, except for one a the top. Siblings Nodes with the same parent. Root Node at the top of a tree.

8 Binary Trees Leaves Internal nodes Depth Nodes with no children.
Nodes that are not leaves. Depth Number of lines along the path back to the root. Root is a depth 0.

9 Binary Trees Level Height
A level of the tree is the set of nodes at a particular depth. Height The height of a tree is the depth of the deepest node.

10 Binary Trees

11 Binary Trees

12 Binary Trees Descendants Proper descendants
A node's descendants are itself, its children, their children, and so on. Every node is a descendant of the root. Proper descendants All of it descendants except itself.

13 Binary Trees Ancestors Proper ancestors
Itself, its parents, its grandparent, and so on. Proper ancestors All of its ancestors except itself.

14 Binary Trees The number of nodes in a binary tree depends on the height of the tree and on how “skinny” or “busy” the tree is. Linear tree Every internal node has only one child. Perfect or Complete All of the leaves are at the same depth. Every internal node has exactly two children.

15 Binary Trees The number of nodes in a binary tree of height h can be any between h + 1 (for a linear tree). (for a perfect binary tree)

16 Binary Trees A binary tree with n nodes log2(n + 1) – 1 (for a perfect binary tree) n-1 (for a linear tree) h Θ(log n) for a perfect binary trees.

17 Binary Trees

18 Binary Trees

19 Binary Trees

20 Binary Trees

21 Binary Trees

22 Binary Trees

23 Binary Trees

24 Binary Trees

25 Binary Trees

26 Binary Trees

27 Binary Trees

28 Tree Traversal Four meaningful orders in which to traverse a binary tree. Preorder Inorder Postorder Level order

29 Tree Traversal

30 Tree Traversal

31 Tree Traversal

32 Tree Traversal

33 Tree Traversal

34 Tree Traversal

35 Tree Traversal Level order traversal is sometimes called breadth-first. The other traversals are called depth-first. Traversal takes Θ(n) in both breadth-first and depth-first. Memory usage in a perfect tree is Θ(log n) in depth-first and Θ(n) in breadth-first traversal.

36 Tree Traversal

37 Summary A tree is a branching, hierarchical structure.
A binary tree either is empty or consists of a node, a left subtree, and a right subtree. A general tree (which cannot be empty) consists of a node and zero or more subtrees.

38 Summary In the widest possible binary tree, called a perfect tree, the number of nodes is exponential in the height of the tree. The height of such a tree is logarithmic in the number of nodes.

39 Summary Binary trees can be traversed Preorder Inorder Postorder
Level order The first three have very elegant recursive algorithms, but level order traversal requires a queue.


Download ppt "Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved."

Similar presentations


Ads by Google