Authored by Tony Feng
Created on March 27th, 2022
Last Modified on March 28th, 2022
Understanding Trees
Main Concepts
- A tree is non-linear and a hierarchical data structure.
- It consists of a collection of nodes such that each node of the tree stores a value and a list of references to their children.
- Different tree data structures allow quicker and easier access to the data as it is a non-linear data structure.
Tree Terminologies
Term | Explanation |
---|---|
Node | 1. A node is an entity that contains a key or value and pointers to its child nodes. 2. Root is the topmost node of a tree. 3. The last nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer to child nodes. 4. The node having at least a child node is called an internal node. 5. The node which is a predecessor of a node is called the parent node of that node. 6. The node which is the immediate successor of a node is called the child node of that node. 7. Any predecessor nodes on the path of the root to that node are called Ancestors of that node. 8. Any successor node on the path from the leaf node to that node are the descendants of the node. 9. Children of the same parent node are called siblings. |
Edge | It is the link between any two nodes. |
Depth of a Node | The depth of a node is the number of edges from the node to the tree’s root node (from bottom to top). |
Height of a Node | 1. The number of edges on the longest path from that node to a leaf (from top to bottom). 2. The height of a Tree is the height of the root node or the depth of the deepest node. |
Level of a node | 1. The level of a node is defined by 1 + the number of connections between the node and the root. 2. The level of the root is 1. |
Degree of a Node | It is the total count of subtrees attached to that node. |
Binary Trees
- Classifications
- Full/ proper/ strict Binary tree
- It can be defined as the tree in which each node must contain 2 children except the leaf nodes.
- Complete Binary tree
- It is a tree in which all the nodes are completely filled except the last level.
- In the last level, all the nodes must be as left as possible.
- Perfect Binary tree
- A tree is a perfect binary tree if all the internal nodes have 2 children.
- All the leaf nodes are at the same level.
- Degenerate Binary tree
- It is a tree in which all the internal nodes have only one children.
- Balanced Binary tree
- The tree, also referred to as a height-balanced binary tree, is defined as a binary tree in which the height of the left and right subtree of any node differ by not more than 1.
- Full/ proper/ strict Binary tree
- Traverse
- Preorder traverse
- Visit root node
- Visit all the nodes in the left subtree
- Visit all the nodes in the right subtree
- Inorder traverse
- First, visit all the nodes in the left subtree
- Then the root node
- Visit all the nodes in the right subtree
- Postorder traverse
- Visit all the nodes in the left subtree
- Visit all the nodes in the right subtree
- Visit the root node
- Preorder traverse
Set Implementation in Python
|
|
Reference
- GreeksforGreeks - Intro to Tree Data Structure
- Height, Depth and Level of a Tree
- Programiz - Balanced Binary Tree
- Binary tree and BinarySearch tree implementation in Python
- 手把手带你刷Leetcode力扣 - 树 Tree
- 手把手带你刷Leetcode力扣 - 树 Tree 补充