Tree_Data_Structures
Tree_Data_Structures
Data
Structure
And
Algorithms
- Trees Data Structure
Prepared by:
Different tree data structures allow quicker and easier access to the data as
it is a non-linear data structure.
Tree Terminologies
Node
A node is an entity that contains a key or value and pointers to its child
nodes.
The last nodes of each path are called leaf nodes or external nodes that do
not contain a link/pointer to child nodes.
The node having at least a child node is called an internal node.
Edge
It is the link between any two nodes.
Root
It is the topmost node of a tree.
Height of a Node
The 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
The depth of a node is the number of edges from the root to the node.
Height of a Tree
The height of a Tree is the height of the root node or the depth of the
deepest node.
Height and depth of each node in a tree
Degree of a Node
The degree of a node is the total number of branches of that node.
Forest
A collection of disjoint trees is called a forest.
Example Visualization:
A (Root)
/\
B C
/\ /\
D E F G (Leaves)
2. Tree Traversal:
Traversal refers to visiting all the nodes in a tree systematically.
d) Level-order Traversal:
- Traverse nodes level by level, starting from the root.
- Example Order: A, B, C, D, E, F, G
3. Binary Tree:
- A binary tree is a tree where each node can have at most two children.
- The two children are usually referred to as the left child and right child.
Example Visualization:
A
/\
B C
/\ /\
D EF G
Example Visualization:
A
/\
B C
/\
D E
Example Visualization:
A
/\
B C
/\ /\
D EF G
Example Visualization:
A
/\
B C
/\ /
D EF
Example Visualization:
Balanced:
A
/\
B C
/\ /
D EF
Not Balanced:
A
/
B
/
C