You are on page 1of 17

What is a binary tree?

A binary tree is a non-linear linked list where each


node may point to two other nodes.

The first node in the structure is called the root
node.

The root node has pointers to two other nodes,
which are called children, or child nodes.

A node that has no children is called a leaf node.

ROOT
LEAF
NODE
Tree structure
Three main types
Preorder
Inorder
Postorder
Preorder
Visit the root.
Traverse the left subtree.
Traverse the right subtree.

Inorder
Traverse the left subtree.
Visit the root.
Traverse the right subtree.

Postorder
Traverse the left subtree.
Traverse the right subtree.
Visit the root.


Three possible cases
Deleting a leaf (node with no children): Deleting a leaf is
easy, as we can simply remove it from the tree.

Deleting a node with one child: Remove the node and
replace it with its child.

Deleting a node with two children: There are two ways
to replace the node :either by the greatest value of the
left subtree or by the smallest value of the right subtree.
1.Removing a leaf. Suppose we want to remove the number 4.

Now lets suppose we want to remove the element 18 which
has two children. We can do it by replacing it with
Max of this subtree
Min of this subtree
or..
In this example we replace the element 18 with the maximum of the left subtree.
NOTE: It would the same correct to replace it with the minimum of the right subtree.
Time Complexity in big O notation

You might also like