Download - Tree Data Structures
![Page 1: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/1.jpg)
Tree Data Structures
![Page 2: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/2.jpg)
Topics to be discussed….
•Trees Data Structures•Trees•Binary Search Trees•Tree traversal•Types of Binary Trees•Threaded binary trees•Applications
![Page 3: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/3.jpg)
Trees Data Structures
Tree◦ Nodes◦ Each node can have 0 or more children◦ A node can have at most one parent
Binary tree◦ Tree with 0–2 children per node
Tree Binary Tree back
![Page 4: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/4.jpg)
Trees Terminology
◦ Root no parent◦ Leaf no child◦ Interior non-leaf◦ Height distance from root to leaf
Root node
Leaf nodes
Interior nodes Height
![Page 5: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/5.jpg)
Level and Depth
K L
E F
B
G
C
M
H I J
D
ALevel
1
2
3
4
node (13)degree of a nodeleaf (terminal)Non terminalparentchildrensiblingdegree of a tree (3)ancestorlevel of a nodeheight of a tree (4)
3
2 1 3
2 0 0 1 0 0
0 0 0
1
2 2 2
3 3 3 3 3 3
4 4 4
![Page 6: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/6.jpg)
Arithmetic Expression Using BT+
*
A
*
/
E
D
C
B
inorder traversalA / B * C * D + Einfix expressionpreorder traversal+ * * / A B C D Eprefix expressionpostorder traversalA B / C * D * E +postfix expressionlevel order traversal+ * E * D / C A B
back
![Page 7: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/7.jpg)
Binary Search Trees
Key property◦ Value at node
Smaller values in left subtree Larger values in right subtree
◦ Example X > Y X < Z
Y
X
Z
back
![Page 8: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/8.jpg)
Tree traversal
Two types of tree traversal are;
1.Recursive
2.Non recursive
back
![Page 9: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/9.jpg)
Preorder: node, left, right
Inorder: left, node, right
Postorder: left, right, node
Recursive traversal
back
![Page 10: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/10.jpg)
In this stack is used to implement the non recursive traversal.
All nodes first put into the stack and then each node is processed according to traversal.
Non recursive traversal
back
![Page 11: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/11.jpg)
Binary Search Trees
Binary search trees
Not a binary search tree
5
10
30
2 25 45
5
10
45
2 25 30
5
10
30
2
25
45
![Page 12: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/12.jpg)
Example Binary Searches Find (root, 25 )
5
10
30
2 25 45
5
10
30
2
25
45
10 < 25, right
30 > 25, left
25 = 25, found
5 < 25, right
45 > 25, left
30 > 25, left
10 < 25, right
25 = 25, found
![Page 13: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/13.jpg)
Types of Binary Trees Degenerate – only one child Complete – always two children Balanced – “mostly” two children
more formal definitions exist, above are intuitive ideas
Degenerate binary tree
Balanced binary tree
Complete binary treeback
![Page 14: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/14.jpg)
Threaded Binary Trees
Two many null pointers in current representationof binary trees n: number of nodes number of non-null links: n-1 total links: 2n null links: 2n-(n-1)=n+1
Replace these null pointers with some useful “threads”.
back
![Page 15: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/15.jpg)
Pre-order traversal while duplicating nodes and values can make a complete duplicate of a binary tree. It can also be used to make a prefix expression (Polish notation) from expression trees: traverse the expression tree pre-orderly.
In-order traversal is very commonly used on binary search trees because it returns values from the underlying set in order, according to the comparator that set up the binary search tree (hence the name).
Post-order traversal while deleting or freeing nodes and values can delete or free an entire binary tree. It can also generate a postfix representation of a binary tree.
Applications
back
![Page 16: Tree Data Structures](https://reader035.vdocuments.net/reader035/viewer/2022071807/56812ba1550346895d8fcd62/html5/thumbnails/16.jpg)
Thank You