Data Structures in Python - Trees
A tree data structure is a non-linear data structure in which a collection of elements known as nodes are connected via edges, resulting in exactly one path between any two nodes. ** What is Tree Data Structure in Python** Like every programming language. In Python a tree, which is a hierarchical data structure, each node is connected by an edge. The tree consists of multiple nodes, with one unique root node serving as the starting point. Trees are often used to represent hierarchical organizations, such as organizational charts or file systems. The topmost node of the tree is called the root, and the nodes below it are called the child nodes. Each node can have multiple child nodes, and these child nodes can also have their child nodes, forming a recursive structure. Basic Terminology of Tree Root Node The Topmost node of a tree is known as the root node. Parents Node A node that has a child node is known as the parent node. Child Node A node that is a descendant of another node is known as a child node. ** Leaf Node** A node without children is known as a leaf node. Subtree A tree consisting of a node and its descendants is known as a subtree. Height The number of edges in the longest path from a node to a leaf node. Depth The number of edges from the root to a node. Types of Tree Data Structure There are three types of tree data structures: Binary Tree A binary Tree is defined as a Tree data structure with at most 2 children. Since each element in a binary tree can have only 2 children, we typically name them the left and right child. ** Ternary Tree** A Ternary Tree is a tree data structure in which each node has at most three child nodes, usually distinguished as “left”, “mid” and “right”. N-ary Tree Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Click here to read complete tutorials
A tree data structure is a non-linear data structure in which a collection of elements known as nodes are connected via edges, resulting in exactly one path between any two nodes.
**
What is Tree Data Structure in Python**
Like every programming language. In Python a tree, which is a hierarchical data structure, each node is connected by an edge. The tree consists of multiple nodes, with one unique root node serving as the starting point. Trees are often used to represent hierarchical organizations, such as organizational charts or file systems.
The topmost node of the tree is called the root, and the nodes below it are called the child nodes. Each node can have multiple child nodes, and these child nodes can also have their child nodes, forming a recursive structure.
Basic Terminology of Tree
Root Node
The Topmost node of a tree is known as the root node.
Parents Node
A node that has a child node is known as the parent node.
Child Node
A node that is a descendant of another node is known as a child node.
**
Leaf Node**
A node without children is known as a leaf node.
Subtree
A tree consisting of a node and its descendants is known as a subtree.
Height
The number of edges in the longest path from a node to a leaf node.
Depth
The number of edges from the root to a node.
Types of Tree Data Structure
There are three types of tree data structures:
Binary Tree
A binary Tree is defined as a Tree data structure with at most 2 children. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.
**
Ternary Tree**
A Ternary Tree is a tree data structure in which each node has at most three child nodes, usually distinguished as “left”, “mid” and “right”.
N-ary Tree
Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes.