Olivine logo
jpnt blog / Journal / Trees

Trees

#programming

A tree is a hierarchical data structure composed of nodes connected by edges. The structure starts with a single root node and extends downwards, forming parent-child relationships among nodes. Each node, except the root, has exactly one parent, and nodes with no children are called leaf nodes. Various concepts and properties characterize trees.

Concepts

Nodes and Edges

Root, Parent, Child, and Leaf

Subtrees and Paths

Ascendants and Descendants

Height and Depth

Degree

Ascendants and Descendants

Ascendents of node 12:

Descendants of node 90:

Height and depth

Binary Trees

A binary tree is a specific type of tree where each node has at most two children: a left child and a right child.

Full and Perfect Binary Trees

Full Binary Tree: Every node is a leaf or has exactly two children.

Perfect Binary Tree: A full binary tree where all leaves have the same depth.

Tree Traversals

Tree traversals are methods for visiting and exploring all nodes in a tree.

Types of Traversals:

Binary Search Tree (BST)

A Binary Search Tree is a binary tree with the property that for each node, all nodes in its left subtree have values less than the node, and all nodes in its right subtree have values greater than the node.

Operations on BST

AVL Tree or Balanced Trees

AVL trees are a type of self-balancing binary search tree where the height difference between the left and right subtrees (balance factor) of any node is at most 1.

AVL Tree Operations

Kd-trees

A Kd-tree, or k-dimensional tree, is a binary tree used for multidimensional searches, such as nearest neighbor or range searches.

2d-tree

A 2d-tree is a specific instance of a Kd-tree where k is 2, representing points in a 2-dimensional space.