CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.
-
Upload
annabella-gordon -
Category
Documents
-
view
241 -
download
1
Transcript of CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.
![Page 1: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/1.jpg)
CHAPTER 06
Compiled by: Dr. Mohammad Omar AlhawaratTrees
![Page 2: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/2.jpg)
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
![Page 3: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/3.jpg)
3
Hierarchical Organization
Example: File Directories
![Page 4: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/4.jpg)
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
![Page 5: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/5.jpg)
5
Example of a Tree
A
C B D
E F G
root
![Page 6: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/6.jpg)
6
Example of a Tree
A
C B D
E F G
rootIn a tree, every pair of linked nodes have a parent-child relationship (the parent is closer to the root)
![Page 7: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/7.jpg)
7
Example of a Tree (Cont.)
A
C B D
E F G
root For example, C is a parent of G
![Page 8: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/8.jpg)
8
Example of a Tree (Cont.)
A
C B D
E F G
root E and F are children of D
![Page 9: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/9.jpg)
9
Example of a Tree (Cont.)
A
C B D
E F G
root The root node is the only node that has no parent.
![Page 10: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/10.jpg)
10
Example of a Tree (Cont.)
A
C B D
E F G
root Leaf nodes (or leaves for short) have no children.
![Page 11: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/11.jpg)
11
Subtrees
A
B C
I K D E F
J G H
subtree
root A subtree is a part of a tree that is a tree in itself
![Page 12: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/12.jpg)
12
Subtrees (Cont.)
A
B C
I K D E F
J G H
subtree
root It normally includes each node reachable from its root.
![Page 13: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/13.jpg)
13
Subtrees (Cont.)
A
B C
I K D E F
J G H
root Even though this looks like a subtree in itself…
![Page 14: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/14.jpg)
14
Binary Trees
A binary tree is a tree in which each node can only have up to two children…
![Page 15: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/15.jpg)
15
Not a Binary Tree
A
B C
I K D E F
J G H
root
![Page 16: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/16.jpg)
16
Example of a Binary Tree
A
B C
I K E
J G H
rootThe links in a tree are often called edges
![Page 17: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/17.jpg)
17
Levels
A
B C
I K E
J G H
rootlevel 0
level 1
level 2
level 3
The level of a node is the number of edges in the path from the root node to this node
![Page 18: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/18.jpg)
18
Full Binary Tree
B
D
H
root
I
A
E
J K
C
F
L M
G
N O
In a full binary tree, each node has two children except for the nodes on the last level, which are leaf nodes
![Page 19: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/19.jpg)
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
![Page 20: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/20.jpg)
20
Complete Binary Trees (Cont.)
B
D
H
root
I
A
E
C
F G
![Page 21: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/21.jpg)
21
Complete Binary Trees (Cont.)
B
D
H
root
I
A
E
J K
C
F
L
G
![Page 22: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/22.jpg)
22
Full Binary Trees
B
D
H
root
I
A
E
J K
C
F
L M
G
N O
A full binary tree is also a complete binary tree.
![Page 23: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/23.jpg)
1-23
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:
max 2 1HN
Full Binary Trees (Cont.)
![Page 24: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/24.jpg)
1-24
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.
Balanced Binary Trees
![Page 25: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/25.jpg)
25
Binary Trees
A binary tree is either empty or has the following form
Where Tleft and Tright are binary trees
![Page 26: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/26.jpg)
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)
![Page 27: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/27.jpg)
27
Binary Trees
![Page 28: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/28.jpg)
28
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.
Recursive definition of a tree
![Page 29: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/29.jpg)
29
Recursive definition of a tree (Cont.)
![Page 30: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/30.jpg)
30
Tree Traversal
Four meaningful orders in which to traverse a binary tree. Preorder Inorder Postorder Level order
![Page 31: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/31.jpg)
31
Tree Traversal (Cont.)
![Page 32: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/32.jpg)
32
In Preorder, the rootis visited before (pre)the subtrees traversals
In Inorder, the root isvisited in-between left and right subtree traversal
In Postorder, the rootis visited after (post)the subtrees traversals
Preorder Traversal:1. Visit the root2. Traverse left subtree3. Traverse right subtree
Inorder Traversal:1. Traverse left subtree2. Visit the root3. Traverse right subtree
Postorder Traversal:1. Traverse left subtree2. Traverse right subtree3. Visit the root
Tree Traversal (Cont.)
![Page 33: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/33.jpg)
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
![Page 34: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/34.jpg)
34
Tree Traversal (Cont.)
Preorder traversal: visit root before the subtrees
![Page 35: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/35.jpg)
35
Tree Traversal (Cont.)
Inorder traversal: visit root between visiting the subtrees
![Page 36: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/36.jpg)
36
Tree Traversal (Cont.)
Postorder traversal: visit root after visiting the subtrees
These are examples of a depth-first traversal.
These are examples of a depth-first traversal.
![Page 37: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/37.jpg)
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.
This is an example of a breadth-first traversal.
![Page 38: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/38.jpg)
38
Tree Traversals – Example 1
![Page 39: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/39.jpg)
39
Assume: visiting a node is printing its label
Preorder: 1 3 5 4 6 7 8 9 10 11 12
Inorder:4 5 6 3 1 8 7 9 11 10 12
Postorder:4 6 5 3 8 11 12 10 9 7 1
1
3
11
98
4 6
5
7
12
10
Tree Traversals – Example 2
![Page 40: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/40.jpg)
40
Assume: visiting a node is printing its data
Preorder: 15 8 2 6 3 711 10 12 14 20 27 22 30
Inorder: 2 3 6 7 8 10 1112 14 15 20 22 27 30
Postorder: 3 7 6 2 10 1412 11 8 22 30 27 20 15
6
15
8
2
3 7
11
10
14
12
20
27
22 30
Tree Traversals – Example 3
![Page 41: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/41.jpg)
41
Tree Traversals – Example 4
![Page 42: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/42.jpg)
42
Examples of Binary Trees
Expression Trees
![Page 43: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/43.jpg)
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.
![Page 44: CHAPTER 06 Compiled by: Dr. Mohammad Omar Alhawarat Trees.](https://reader035.fdocuments.in/reader035/viewer/2022081501/56649f135503460f94c27c20/html5/thumbnails/44.jpg)
45
Questions?
Questions ?