data structures and algorithms unit-3 trees prepared by m v brahmananda reddy
TRANSCRIPT
![Page 1: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/1.jpg)
Chapter 4: TreesChapter 4: Trees
General Tree ConceptsBinary Trees
GITAM UNIVERSITY M V B REDDY
Data Structures and Algorithm
![Page 2: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/2.jpg)
2
Trees
DefinitionsRepresentationBinary treesTraversalsExpression trees
![Page 3: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/3.jpg)
3
Definitions
tree - a non-empty collection of vertices & edgesvertex (node) - can have a name and carry other associated informationpath - list of distinct vertices in which successive vertices are connected by edges
![Page 4: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/4.jpg)
4
Definitions
any two vertices must have one and only one path between them else its not a tree
a tree with N nodes has N-1 edges
![Page 5: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/5.jpg)
5
Definitions
root - starting point (top) of the tree
parent (ancestor) - the vertex “above” this vertex
child (descendent) - the vertices “below” this vertex
![Page 6: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/6.jpg)
6
Definitions
leaves (terminal nodes) - have no children
level - the number of edges between this node and the root
ordered tree - where children’s order is significant
![Page 7: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/7.jpg)
7
Definitions
Depth of a node - the length of the path from the root to that node• root: depth 0
Height of a node - the length of the longest path from that node to a leaf• any leaf: height 0
Height of a tree: The length of the longest path from the root to a leaf
![Page 8: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/8.jpg)
8
Balanced Trees
the difference between the height of the left sub-tree and the height of the right sub-tree is not more than 1.
![Page 9: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/9.jpg)
9
Trees - Example
E
R
T
ELPM
EA
SA
root
Leaves or terminal nodes
Child (of root)
Depth of T: 2
Height of T: 1
Level
0
1
3
2
![Page 10: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/10.jpg)
10
Tree Representation
Class TreeNode { Object element; TreeNode firstChild; TreeNode nextSibling; }
![Page 11: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/11.jpg)
11
Example
a
b fe
c d g
a
b e
c d
f
g
![Page 12: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/12.jpg)
12
Binary Tree
S
A
B
N
O
N
P
D
M
I
S Internal node
External node
![Page 13: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/13.jpg)
13
Height of a Complete Binary Tree
L 0
L 1
L 2
L 3
At each level the number of the nodes is doubled. total number of nodes: 1 + 2 + 22 + 23 = 24 - 1 = 15
![Page 14: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/14.jpg)
14
Nodes and Levels in a Complete Binary Tree
Number of the nodes in a tree with M levels:
1 + 2 + 22 + …. 2M = 2 (M+1) - 1 = 2*2M - 1
Let N be the number of the nodes.
N = 2*2M - 1, 2*2M = N + 12M = (N+1)/2M = log( (N+1)/2 )
N nodes : log( (N+1)/2 ) = O(log(N)) levelsM levels: 2 (M+1) - 1 = O(2M ) nodes
![Page 15: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/15.jpg)
15
Binary Tree Node
Class BinaryNode
{
Object Element; // the data in the node
BinaryNode left; // Left child
BinaryNode right; // Right child
}
![Page 16: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/16.jpg)
16
Binary Tree – Preorder Traversal
C
LR
E
T
D
O
N
U
M
P
A
Root Left Right
First letter - at the root
Last letter – at the rightmost node
![Page 17: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/17.jpg)
17
Preorder Algorithm
preorderVisit(tree){ if (current != null) { process (current);
preorderVisit (left_tree);preorderVisit (right_tree);
}}
![Page 18: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/18.jpg)
18
Binary Tree – Inorder Traversal
U
AE
R
T
N
P
D
M
O
C
L
LeftRootRight
First letter - at the leftmost node
Last letter – at the rightmost node
![Page 19: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/19.jpg)
19
Inorder Algorithm
inorderVisit(tree){ if (current != null) {
inorderVisit (left_tree); process (current);
inorderVisit (right_tree); }}
![Page 20: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/20.jpg)
20
Binary Tree – Postorder Traversal
D
LU
A
N
E
P
R
O
M
C
T
LeftRightRoot
First letter - at the leftmost node
Last letter – at the root
![Page 21: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/21.jpg)
21
Postorder Algorithm
postorderVisit(tree){ if (current != null) {
postorderVisit (left_tree);postorderVisit (right_tree);process (current);
}}
![Page 22: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/22.jpg)
22
Expression Trees
1 2
The stack contains references to tree nodes (bottom is to the left)
+
1 2
3*
+
1 2
3
(1+2)*3
Post-fix notation: 1 2 + 3 *
![Page 23: DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY](https://reader035.vdocument.in/reader035/viewer/2022062513/55620924d8b42a7d028b490f/html5/thumbnails/23.jpg)
23
Expression Trees
In-order traversal:
(1 + 2) * ( 3)
Post-order traversal:
1 2 + 3 *
*
+
1 2
3