Trees
Trees
A Tree Abstract Data Type (ADT) is a non-linear data structure that organizes data in a
hierarchical manner.
Tree Terminology:
Child – A node directly connected to another node when moving away from the Root.
Descendant – Any successor node on the path from root to that node.
Ancestor – Any predecessor node on the path from root to that node
Level – The level of a node is defined by 1 + (the number of connections between the node
and the root).
Height of node – The height of a node is the number of edges on the longest downward
path between that node and a leaf.
Height of tree – The height of a tree is the height of its root node.
Depth – The depth of a node is the number of edges from the node to the tree's root node.
A Binary Tree is a type of tree data structure where each node can have a maximum of two
child nodes, a left child node and a right child node.
This restriction, that a node can have a maximum of two child nodes.
If each node of binary tree has either two children or no child at all.
A perfect binary tree in which all leaf nodes are at the same level.
COMPLETE BINARY TREE:
A complete binary tree is a binary tree in which every level except possibly the last node is
completely filled from left to right leaving no gaps.
If a tree which is dominated by left child node or right child node is said to be a skewed
binary tree.
Each node of a binary tree has the following 3 parts:
Data
Binary tree traversal refers to the process of visiting each node in the tree exactly once in a
systematic way. There are four main types of binary tree traversals: in-order, pre-order, post-
order, and level-order. Each traversal method visits nodes in a different order.
In in-order traversal, the nodes are recursively visited in this order: left subtree, root node,
and then the right subtree.
Example:
In-order Traversal: 4, 2, 5, 1, 3
Steps:
In pre-order traversal, the nodes are recursively visited in this order: root node, left subtree,
and then the right subtree
Example:
Pre-order Traversal: 1, 2, 4, 5, 3
Steps:
In post-order traversal, the nodes are recursively visited in this order: left subtree, right
subtree, and then the root node.
Example:
Post-order Traversal: 4, 5, 2, 3, 1
Steps:
In level-order traversal, the nodes are visited level by level from left to right. This traversal
uses a queue data structure to keep track of nodes.
Example:
Level-order Traversal: 1, 2, 3, 4, 5
Steps:
Using Arrays
i=0,2i+2=2
i=0,2i+1=1
C
B
i=1,2i+1=3 i=1,2i+2=4
D E
i=3,2i+1=7 i=4,2i+1=9
F
G
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
A B C D E - - F - G
A binary search tree (BST) is a type of data structure used in computer science to store and
manage data. In a BST, each node is a part of the tree that holds a value. Each node can have
up to two children nodes. The node at the top is called the root.
All the values in the left child and its descendants are smaller than the node's value.
All the values in the right child and its descendants are greater than the node's value.
This structure helps to quickly find, add, or remove values from the tree.
For example, if you want to find a value, you can start at the root and move left or right
depending on whether the value is smaller or larger than the current node, making the
search very efficient.
Example:
The left child of 10 is 5, and all values in the left subtree (3, 5, 7) are less than 10.
The right child of 10 is 15, and all values in the right subtree (15, 20) are greater than
10.
1. Insertion
Insertion in binary search tree includes adding a new node in a way that maintains the BST
property: for any node, all values in its left subtree are smaller, and all values in its right
subtree are greater.
Algorithm:
Example:
Step-by-Step Insertion:
Result:
3. Searching
Searching in a binary search tree involves finding a node with a given value by leveraging the
BST property to reduce the number of comparisons.
Algorithm:
Repeat steps 2-5 until you find the value or reach an empty spot (unsuccessful
search).
8 is equal to 8, search is
successful.