Trees in Data Structure:
...

A tree is a hierarchical data structure consisting of nodes connected by edges. It is a widely-used data structure in computer science and is used in many algorithms and applications.

Basic Terminology:
...

  • Node: A single element in a tree.
  • Edge: A line connecting two nodes.
  • Root: The topmost node in a tree.
  • Parent: A node that has one or more child nodes.
  • Child: A node that is connected to a parent node by an edge.
  • Leaf: A node that has no child nodes.
  • Height: The number of edges on the longest path from the root to a leaf node.

Types of Trees:
...

There are many different types of trees, each with its own unique characteristics. Here are some of the most common types of trees:

Free Tree:
...

A free tree is a tree where each node can have any number of child nodes. In other words, there are no restrictions on the number of child nodes a node can have. Free trees are also known as general trees.

Rooted Tree:
...

A rooted tree is a tree where one node is designated as the root node. The root node is typically drawn at the top of the tree. All other nodes in the tree are either parent nodes, child nodes, or leaf nodes.

Binary Tree:
...

A binary tree is a tree where each node can have at most two child nodes. The child nodes are typically referred to as the left child and the right child. Binary trees are commonly used in data structures such as binary search trees and heaps.

Binary Search Tree:
...

A binary search tree is a binary tree where the left child of a node is less than the node and the right child of a node is greater than the node. This property makes binary search trees useful for searching and sorting operations.

AVL Tree:
...

An AVL tree is a binary search tree where the difference in height between the left and right subtrees of any node is at most one. This property ensures that the tree is balanced, which leads to faster search and insertion times.

Red-Black Tree:
...

A red-black tree is a binary search tree with the following properties:

  • Each node is either red or black.
  • The root node is black.
  • All leaf nodes (i.e., null nodes) are black.
  • If a node is red, then both of its child nodes are black.
  • Every path from a node to a leaf node contains the same number of black nodes.

These properties ensure that the tree is balanced and that the worst-case time complexity for operations such as search and insertion is O(log n).

Heights, Depth and Descendants:
...

As mentioned earlier, the height of a node in a tree is the number of edges on the longest path from the node to a leaf node. The height of a tree is the height of the root node, also the depth of a node is the length of the path to root node

The number of descendants of a node is the total number of nodes in its subtree, including the node itself. The descendants of a node can be found by traversing its subtree recursively and counting the nodes.

Relationships between Nodes:
...

In addition to parent-child relationships, there are some other relationships that can exist between nodes in a tree:

  • Ancestor-Descendant Relationship: A node is an ancestor of another node if it is on the path from the root to the other node. Conversely, a node is a descendant of another node if the other node is on the path from the root to the node.
  • Sibling Relationship: Two nodes are siblings if they have the same parent node.
  • Grandparent-Grandchild Relationship: A node is a grandparent of another node if it is the parent of the parent node of the other node. Conversely, a node is a grandchild of another node if it is the child of the child node of the other node.

Balanced Trees:
...

Balanced trees are trees where the depth of the leaves are less than or equal to 1.

Ordered tree:
...

An ordered tree (or plane tree) is a rooted tree in which an ordering is specified for the children of each vertex.

Forest:
...

A forest is an undirected graph in which any two vertices are connected by at most one path.

Traversing a Tree:
...

Traversing binary tree.svg

  1. Preorder Traversal :
  2. Postorder Traversal:
  3. Inorder Traversal:

if you have the Postorder and Preorder of a tree you can construct the tree recursively.

Convert to binary tree:
...

connect each root to the most left descendants and the most left node to all its siblings.

Convert to binary tree.svg

  • preorder of both trees are the same.
  • Postorder is different between them.