0% found this document useful (0 votes)
20 views

8.0 Tree Data Structure - Notes

The document discusses tree data structures including binary trees. It defines key tree terminology like nodes, edges, root, and describes tree traversal methods like breadth-first, preorder, inorder and postorder. It also covers different types of binary trees and provides an example implementation of a binary tree in Python.

Uploaded by

ssamyang92
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

8.0 Tree Data Structure - Notes

The document discusses tree data structures including binary trees. It defines key tree terminology like nodes, edges, root, and describes tree traversal methods like breadth-first, preorder, inorder and postorder. It also covers different types of binary trees and provides an example implementation of a binary tree in Python.

Uploaded by

ssamyang92
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Tree Data Structure

Tree Data Structure


• Tree is a nonlinear hierarchical data structure
that consists of nodes connected by edges.
Node
Edge
Root
Parent
Child
Leaf
Height of a Node
Depth of a Node
Height of a Tree
Tree Terminologies
• Node is an entity that contains a key or value and pointers to its child nodes.
• Edge is the link between any two nodes.
• Root is the topmost node of a tree.
• Parent any node except the root node has one edge upward to a node called parent.
• Child is the node below a given node connected by its edge downward is called its
child node.
• Leaf is the node which does not have any child node is called the leaf node.
• Height of a Node is the number of edges from the node to the deepest leaf (ie. the
longest path from the node to a leaf node).
• Depth of a Node is the number of edges from the root to the node.
• Height of a Tree is the height of the root node or the depth of the deepest node.
Tree Terminologies

Node

Edge
Binary Tree
• Binary Tree is a special data structure used for data storage purposes.
• A binary tree has a special condition that each node can have a
maximum of two children.
• A binary tree has the benefits of both an ordered array and a linked
list as search is as quick as in a sorted array and insertion or deletion
operation are as fast as in linked list.
Tree Traversal

• process of visiting all the nodes in a tree exactly once in a particular


order.
• By visit, means reading or processing data stored in a node.
• start from the root (head) node.

There are two approaches used to traverse a tree −


• Breadth-first
• Depth-first
Tree Traversal

• Breadth-first approach (Level-order)


visit all the nodes at the same depth or level before visiting the nodes
at the next level.

• Depth-first approach
identifies the root node and visits all the grandchildren of the left then
right sub-tree or the right sub-tree then the left sub-tree

• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Tree Traversal

• Breadth-first approach (Level-


order)
visit all the nodes at the same
depth or level before visiting
the nodes at the next level.

A→B→C→D→E→F→G
In-Order Traversal
• In this traversal method, the left subtree is visited
first, then the root and later the right sub-tree.
• We should always remember that every node may
represent a subtree itself.
• We start from A, and following in-order traversal, we
move to its left subtree B. B is also traversed in-
order.
• The process goes on until all the nodes are visited.
The output of inorder traversal of this tree will be −

D→B→E→A→F→C→G
In-Order Traversal

Until all nodes are traversed −


• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
Pre-Order Traversal
• In this traversal method, the root node is visited
first, then the left subtree and finally the right
subtree.

• We start from A, and following pre-order traversal,


we first visit A itself and then move to its left
subtree B. B is also traversed pre-order.

• The process goes on until all the nodes are visited.


The output of pre-order traversal of this tree will
be −

A→B→D→E→C→F→G
Pre-order Traversal

Until all nodes are traversed −


• Step 1 − Visit root node.
• Step 2 − Recursively traverse left subtree.
• Step 3 − Recursively traverse right subtree.
Post-order Traversal
• In this traversal method, the root node is visited last.

• First we traverse the left subtree, then the right subtree


and finally the root node.

• We start from A, and following Post-order traversal, we


first visit the left subtree B. B is also traversed post-
order. The process goes on until all the nodes are visited.
The output of post-order traversal of this tree will be −

• D→E→B→F→G→C→A
Post-order Traversal

Until all nodes are traversed −


• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
• Step 3 − Visit root node.
Exercises on Tree Traversal
3 Determine the Breadth-first, In-order, Pre-order and Post-order

6 2 Breadth-f:
Pre-order:
In-order:
8 4 7 Post-order:

5 9
Exercises on Tree Traversal
j Determine the Breadth, In-order, Pre-order and Post-order

f e Pre-order:
In-order:
Post-order:
d a i Breadth:

g b
Types of Binary Tree
1. Full Binary Tree if every node has 0 or 2 children.

1 1

2 3 2 3

4 5 6 7 4 5
Types of Binary Tree
2. Complete Binary Tree if all the levels are completely filled except
possibly the last level and the last level has all keys as left as
possible.
1

2 3

4 5 6
Types of Binary Tree
3. Perfect Binary Tree if all the internal nodes have two children and
all leaf nodes are at the same level.
Types of Binary Tree
4. Balanced Binary Tree if the height of the left and right sub-tree of
any node differ by not more than 1.
diff = | height of left child – height of right child |

diff = 1 diff = 2

diff = 1 diff = 0 diff = 1 diff = 0

diff = 1 diff = 0 diff = 0 diff = 0 diff = 1 diff = 0

diff = 0 diff = 0
Types of Binary Tree
5. Degenerate Binary Tree if every internal node has one child.
Binary Tree Implementation
1. class Node:
2. def __init__(self,key):
3. self.left = None
4. self.right = None
5. self.val = key
Binary Tree Implementation
root

1. class Node: 1
2. def __init__(self,key):
3. self.left = None
4. self.right = None 2 3
5. self.val = key
6.
7. root = Node(1) 4
8. root.left = Node(2);
9. root.right = Node(3);
10. root.left.left = Node(4);

You might also like