notesCHAPTER_5_tree_data_structure_ds.pppt

ssuserf265e9 9 views 33 slides Oct 14, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

data structure, tree


Slide Content

T opic 5 TREE DFC 30233 DATA STRUCTURE

1.0 Define tree,binary tree and binary search tree BINARY TREE A type of data structure where each parent node can have at most two child nodes These two child nodes are known as the  left child  and  right child .

1.0 Define tree,binary tree and binary search tree BINARY SEARCH TREE A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions ; No duplicate values Left child node is smaller than its parent Node Right child node is greater than its parent Node

DID YOU KNOW?

― Irene M. Pepperberg

1.2 Terminology in relation to trees: Root , Parent , Children , Leaves, Sibling , Node , Path , Degree , Level

Root ROOT NODE - the first node root node is the origin of tree data structure must be only one root node Terminology: 1. Root

Parent Node PARENT NODE - the node which is predecessor of any node the node which has branch from it to any other node Terminology: 2. Parent

Child Child Node - the node which is descendant of any node the node which has a link from its parent node any parent node can have any number of child nodes all the nodes except root are child nodes Terminology: 3. Children

Leaf LEAF Node - the node which does not have a child leaf node is also called as Terminal node Terminology: 4. Leaf

Siblings SIBLINGS - nodes which belong to same Parent Terminology: 5. Siblings

Internal Nodes INTERNAL Node - the node which has at least one child Internal nodes are also called as ' Non-Terminal ' nodes Terminology: 6. Internal Nodes

Path PATH - the sequence of Nodes and Edges from one node to another node Length of a Path is total number of nodes in that path. Example: the path A - B - E - J has length 4 Terminology: 7. Path

Degree PATH - the sequence of Nodes and Edges from one node to another node Length of a Path is total number of nodes in that path. Example: the path A - B - E - J has length 4 Terminology: 8. Degree

Edge EDGE - the connecting link between any two nodes In a tree with ' N ' number of nodes there will be a maximum of ' N-1 ' number of edges. Terminology: 9. Edge

Level LEVEL - each step from top to bottom the root node - Level the children of root node - Level 1 Terminology: 10. Level

Height height of all leaf nodes is '0' Terminology: 11. Height

Depth height of all leaf nodes is '0' Terminology: 12. Depth

Terminology: 13. Sub Tree

2.0 Binary Tree. Binary Tree is a tree in which every node can have a maximum of two children. Every node can have either children or 1 child or 2 children but not more than 2 children.

Full Binary Tree Binary Tree

Perfect Binary Tree Complete Binary Tree obtained from a perfect binary tree by deleting consecutive leaves of the tree from right to left 23

Arithmetic Expression

Example: arithmetic expression tree for the expression (2 × (a - 1)) + (3 × b) :

Infix, Prefix and Postfix traversal An infix expression , the operator appears between its operands. In a prefix expression, places a binary operator before its operands. In a postfix expression, the binary operators comes after its operands.

Example 1 S t a t e the o r de r o f node s v i s i t ed us i n g p r e f i x , i n f i x and postfix traversal. Prefix: - 8 / 4 2 Infix: 8 – ( 4 / 2 ) Postfix: 8 4 2 / -

Example 2 State the order of nodes visited using prefix, infix and postfix traversal. Infix form: Prefix form : Postfix form :

(x+xy) + (x/y) x+ ((xy+x)/y) Represent the expressions using binary trees. Write these expressions in prefix notation. Write these expressions in postfix notation. Write these expressions in infix notation.

Binary Tree Representations A binary tree data structure is represented using two methods. Those methods are as follows. Array Representation Linked List Representation Consider the following binary tree.

1. Array Representation 2. Linked List Representation