User:Epoon/sandbox

Binary Trees
Binary trees are rooted trees constructed by nodes in which the parent node is connected to at most two children nodes.

Properties and Terminology

 * The topmost node is called the root of the tree.
 * The nodes that are directly under a node are called the children.
 * The node directly above the node or nodes is called the parent.
 * Nodes with the same parents are considered siblings.
 * Since each node has at most two children, they are categorized as the left child and the right child.
 * A node can have both the left child and the right child, either the left child or the right child, or no children.
 * Every node can have at most one left child and at most one right child.
 * Leaf or external nodes are nodes without children.
 * Internal nodes are nodes with children.
 * A left child is null if the parent does not have a left child. The rule also applies when there is no right child.
 * A left subtree is a binary tree on the left child of the root node.
 * A right subtree is a binary tree on the right child of the root node.

Let height h be the number of nodes on the path from the root to the leaf,


 * The minimum number of nodes in a binary tree of height h is equal to h + 1.
 * The maximum number of nodes in a binary tree of height h is equal to 2h+1–1 when the height of the root is considered as zero. The maximum is equal to 2h–1 when the height of the root is considered as 1.

Full Binary Tree
A full binary tree is a binary tree that has exactly zero or two children. The number of leaf nodes is equal to the number of internal nodes plus one.

Complete Binary Tree
A complete binary tree is a binary tree in which all levels are completely filled with the possible exception of the bottom level and all nodes are as left as possible.

Perfect Binary Tree
A perfect binary tree is a binary tree in which each level contains the maximum number of nudes. In other words, the internal nodes have two children and all the leaf nodes are at the same level.

Properties of Perfect Binary Tree:

Let height h be the number of nodes on the path from the root to the leaf,


 * A perfect binary tree of height h has 2h+1–1 nodes.
 * The number of leaf nodes in a perfect binary tree of height h is equal to 2h.
 * The number of internal nodes in a perfect binary tree of height h is equal to 2h–1.
 * The height of a perfect binary tree with nodes n is equal to log2(n+1)–1

Balanced Binary Tree
A balanced binary tree is a binary tree in which the left subtree and the right subtree of every node differ in height by no more than one.

Degenerate (or Pathological) Tree
A degenerate tree is a binary tree in which every internal node has one child, hence it performs similar to a linked list data structure.

Infinite Complete Binary Tree
An infinite tree is a binary tree in which all nodes have two children and the node is countable infinite and the infinite path from the root is uncountable.

Traversals
Traversal is a process in which all nodes are visited.

Depth First Traversals:


 * Inorder traversal visits the left subtree first, then the root, and the right subtree last. It is used to output nodes in ascending order
 * Preorder traversal visits the root node first, then the left subtree, and the right subtree last. It is used to create a copy of the tree or to get prefixes of an expression.
 * Postorder traversal visits the left subtree first, then the right subtree, and the root node last. It is used to delete the tree.

Breadth First or Level Order Traversal:


 * It visits the nodes level by level from left to right.

Insertion in a Binary Tree
To insert a node into the binary tree, search through the tree using breadth first traversal to locate null values. If a left child is empty, insert a new element as the left child. If a right child is empty, insert a new element as the right child.

Deletion in a Binary Tree
When deleting a node from a binary tree, the deleted node replaces the bottommost and rightmost node. To delete a node, locate the bottom right node and the to be deleted node. Replace the bottom right node with the to be deleted node and delete the original bottom right node.