knowt logo

Binary Tree Traversal

Binary Tree Traversal

Traversal refers to visiting each node in a tree in a specific order.

Inorder Traversal

In inorder traversal, the left subtree is traversed first, followed by visiting the current node, and then traversing the right subtree.

Preorder Traversal

In preorder traversal, the current node is visited first, followed by traversing the left subtree and then the right subtree.

Postorder Traversal

In postorder traversal, the left subtree is traversed first, followed by traversing the right subtree, and finally visiting the current node.

Insertion

To insert a new node into a binary tree, we follow these steps:

  1. If the tree is empty, create a new node and make it the root.

  2. Otherwise, traverse the tree to find the appropriate position for the new node.

  3. If the current node has an available left child, insert the new node as the left child.

  4. If the current node has an available right child, insert the new node as the right child.

  5. Repeat steps 3 and 4 until the new node is inserted.

Deletion

Deleting a node from a binary tree involves three different cases:

  1. Deleting a leaf node: Simply remove the node from the tree.

  2. Deleting a node with one child: Replace the node with its child.

  3. Deleting a node with two children: Find the inorder successor or predecessor, replace the node with it, and delete the successor/predecessor node.

Searching

To search for a specific value in a binary tree, we follow these steps:

  1. Start at the root node.

  2. If the current node is null or contains the desired value, return the current node.

  3. If the desired value is less than the current node's value, move to the left child.

  4. If the desired value is greater than the current node's value, move to the right child.

  5. Repeat steps 2-4 until a match is found or the tree is fully traversed.

Root

is the topmost node. It serves as the starting point for accessing the entire tree.

FG

Binary Tree Traversal

Binary Tree Traversal

Traversal refers to visiting each node in a tree in a specific order.

Inorder Traversal

In inorder traversal, the left subtree is traversed first, followed by visiting the current node, and then traversing the right subtree.

Preorder Traversal

In preorder traversal, the current node is visited first, followed by traversing the left subtree and then the right subtree.

Postorder Traversal

In postorder traversal, the left subtree is traversed first, followed by traversing the right subtree, and finally visiting the current node.

Insertion

To insert a new node into a binary tree, we follow these steps:

  1. If the tree is empty, create a new node and make it the root.

  2. Otherwise, traverse the tree to find the appropriate position for the new node.

  3. If the current node has an available left child, insert the new node as the left child.

  4. If the current node has an available right child, insert the new node as the right child.

  5. Repeat steps 3 and 4 until the new node is inserted.

Deletion

Deleting a node from a binary tree involves three different cases:

  1. Deleting a leaf node: Simply remove the node from the tree.

  2. Deleting a node with one child: Replace the node with its child.

  3. Deleting a node with two children: Find the inorder successor or predecessor, replace the node with it, and delete the successor/predecessor node.

Searching

To search for a specific value in a binary tree, we follow these steps:

  1. Start at the root node.

  2. If the current node is null or contains the desired value, return the current node.

  3. If the desired value is less than the current node's value, move to the left child.

  4. If the desired value is greater than the current node's value, move to the right child.

  5. Repeat steps 2-4 until a match is found or the tree is fully traversed.

Root

is the topmost node. It serves as the starting point for accessing the entire tree.

robot