Download presentation

1
**Compiled by: Dr. Mohammad Omar Alhawarat**

Chapter 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees

2
Definition of Tree A tree is a set of linked nodes, such that there is one and only one path from a unique node (called the root node) to every other node in the tree

3
**Hierarchical Organization**

Example: File Directories

4
Tree Terminology Nodes at a given level are children of nodes of previous level Node with children is the parent node of those children Nodes with same parent are siblings Node with no children is a leaf node The only node with no parent is the root node All others have one parent each

5
Example of a Tree root A C B D G E F

6
Example of a Tree In a tree, every pair of linked nodes have a parent-child relationship (the parent is closer to the root) root A C B D G E F

7
**Example of a Tree (Cont.)**

For example, C is a parent of G root A C B D G E F

8
**Example of a Tree (Cont.)**

E and F are children of D root A C B D G E F

9
**Example of a Tree (Cont.)**

The root node is the only node that has no parent. root A C B D G E F

10
**Example of a Tree (Cont.)**

Leaf nodes (or leaves for short) have no children. root A C B D G E F

11
**Subtrees root A subtree is a part of a tree that is a tree in itself A**

C I K D E F J G H subtree

12
Subtrees (Cont.) root It normally includes each node reachable from its root. A B C I K D E F J G H subtree

13
**Subtrees (Cont.) root Even though this looks like a subtree in itself…**

D E F J G H

14
Binary Trees A binary tree is a tree in which each node can only have up to two children…

15
Not a Binary Tree root A B C I K D E F J G H

16
**Example of a Binary Tree**

root A The links in a tree are often called edges B C I K E J G H

17
Levels The level of a node is the number of edges in the path from the root node to this node root level 0 A level 1 B C level 2 I K E level 3 J G H

18
**Full Binary Tree root A B C D E F G H I J K L M N O**

In a full binary tree, each node has two children except for the nodes on the last level, which are leaf nodes

19
Complete Binary Trees A complete binary tree is a binary tree that is either a full binary tree OR a tree that would be a full binary tree but it is missing the rightmost nodes on the last level

20
**Complete Binary Trees (Cont.)**

root A B C D E F G H I

21
**Complete Binary Trees (Cont.)**

root A B C D E F G H I J K L

22
**Full Binary Trees A full binary tree is also a complete binary tree.**

root A B C D E F G H I J K L M N O

23
**Full Binary Trees (Cont.)**

The formula for the maximum number of nodes is derived from the fact that each node can have only two descendants. Given a height of the binary tree, H, the maximum number of nodes in the full binary tree is given as follows:

24
Balanced Binary Trees To Assure that a binary tree is Balanced one the following algorithms is used: AVL Trees. Red-Black Trees. 2-3 Trees and the general case (B-Trees) B-Trees are used with Fetching data from Large Databases.

25
**Binary Trees A binary tree is either empty or has the following form**

Where Tleft and Tright are binary trees

26
Binary Trees Every nonleaf in a full binary tree has exactly two children A complete binary tree is full to its next-to-last level Leaves on last level filled from left to right The height of a binary tree with n nodes that is either complete or full is log2(n + 1)

27
Binary Trees

28
**Recursive definition of a tree**

A tree is a set of nodes that either: is empty or has a designated node, called the root, from which hierarchically descend zero or more subtrees, which are also trees.

29
**Recursive definition of a tree (Cont.)**

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

31
Tree Traversal (Cont.)

32
**Tree Traversal (Cont.) In Preorder, the root is visited before (pre)**

the subtrees traversals In Inorder, the root is visited in-between left and right subtree traversal In Postorder, the root is visited after (post) Preorder Traversal: Visit the root Traverse left subtree Traverse right subtree Inorder Traversal: Traverse left subtree Visit the root Traverse right subtree Postorder Traversal: Traverse left subtree Traverse right subtree Visit the root

33
**Tree Traversal (Cont.) Visiting a node**

Processing the data within a node This is the action performed on each node during traversal of a tree A traversal can pass through a node without visiting it at that moment For a binary tree Visit the root Visit all nodes in the root's left subtree Visit all nodes in the root's right subtree

34
Tree Traversal (Cont.) Preorder traversal: visit root before the subtrees

35
Tree Traversal (Cont.) Inorder traversal: visit root between visiting the subtrees

36
Tree Traversal (Cont.) Postorder traversal: visit root after visiting the subtrees These are examples of a depth-first traversal.

37
Tree Traversal (Cont.) Level-order traversal: begin at the root, visit nodes one level at a time This is an example of a breadth-first traversal.

38
**Tree Traversals – Example 1**

39
**Tree Traversals – Example 2**

Assume: visiting a node is printing its label Preorder: Inorder: Postorder: 1 3 11 9 8 4 6 5 7 12 10

40
**Tree Traversals – Example 3**

Assume: visiting a node is printing its data Preorder: Inorder: Postorder: 6 15 8 2 3 7 11 10 14 12 20 27 22 30

41
**Tree Traversals – Example 4**

42
**Examples of Binary Trees**

Expression Trees

43
**Example: Expression Trees**

Cpt S 223 Example: Expression Trees Store expressions in a binary tree Leaves of tree are operands (e.g., constants, variables) Other internal nodes are unary or binary operators Used by compilers to parse and evaluate expressions Arithmetic, logic, etc. E.g., (a + b * c)+((d * e + f) * g) 43 43 43 Washington State University

44
**Tree Traversal - Summary**

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.

45
Questions? Questions ?

Similar presentations

© 2021 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google