You are on page 1of 6

TREES:

Basic Terminology,

Binary Tree- Properties,

Converting Tree to Binary Tree,

Representation using Sequential and Linked organization,

Binary tree creation and Traversals,

Operations on binary tree.

Binary Search Tree (BST) and its operations,

Threaded binary tree- Creation and Traversal of In-order Threaded Binary tree.

Case Study- Expression tree

TREES:

Definition: A tree data structure is a hierarchical data structure that consists of nodes
connected by edges.

Widely used in computer science for organizing and storing data efficiently.

Nodes:

Basic building blocks of a tree.


Each node contains data and references to its child nodes (if any).
Special nodes: Root (topmost node), leaves (nodes with no children).

Edges:

Connections between nodes.


Represent relationships between nodes.
Root:

Topmost node in a tree.


The starting point for traversing the tree.
Ancestors of a node:

all the nodes along the path from root to that node

Descendant of a node

child, grandchild, grand-grandchild, etc.

Parent and Child Nodes:

Parent node: A node with one or more child nodes.


Child node: A node directly connected to its parent node.
Depth and Height:
Depth: The level of a node in the tree hierarchy, starting from the root (root has depth
0).
Height: The maximum depth of the tree, i.e., the longest path from the root to a leaf
node.

Subtree:

Tree consisting of a node and its descendants

Binary Trees:

A special type of tree where each node can have two, zero, one children.
Common operations: Insertion, deletion, and traversal (in-order, pre-order, post-
order).

Balanced vs. Unbalanced Trees:

Balanced trees: Height is kept minimal, ensuring efficient operations (e.g., AVL trees,
Red-Black trees).
Unbalanced trees: Height can be significantly larger, leading to slower operations.
Applications:

File systems: Representing directories and files.


Database indexing: Efficient searching and retrieval.
Expression evaluation: Parsing mathematical expressions.
Hierarchical data representation: Organizing hierarchical data like XML or JSON.
Advantages:

Efficient for hierarchical data organization.


Allows quick search, insertion, and deletion operations when balanced.
Versatile and applicable in various domains of computer science.

Disadvantages:

Complex to implement compared to linear data structures.


Requires additional memory for storing pointers/references.
Operations may become slow in unbalanced trees

Binary tree:
A special type of tree where each node can have two, zero, one children. which are
referred to as the left child and the right child. which are referred to as the left child
and the right child.

Common operations: Insertion, deletion, and traversal (in-order, pre-order, post-order)

The maximum number of nodes on level i of a binary tree is 2i-1, i>=1.

The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.


Full binary tree:
A full binary tree is a binary tree in which all the nodes have either 0 or 2 offspring

OR

Full binary tree in which except leaf nodes all nodes should have two offspring

A Full binary tree of depth K is a binary tree of depth having 2k-1 nodes k>=0

Let, i be the number of internal nodes


n be the total number of nodes
l be number of leaves
λ be number of levels

Then,

The number of leaves is (i + 1).


The total number of nodes is (2i + 1).
The number of internal nodes is (n – 1) / 2.
The number of leaves is (n + 1) / 2.
The total number of nodes is (2l – 1).
The number of internal nodes is (l – 1).
The number of leaves is at most (2λ – 1).

Complete binary tree:


A binary tree is said to be a complete binary tree if all its levels, except possibly the
last level, have the maximum number of possible nodes, and all the nodes in the last
level appear as far left as possible.

There are 2 points that you can recognize from here,


1. The leftmost side of the leaf node must always be filled first.
2. It isn’t necessary for the last leaf node to have a right sibling

Skewed Binary Tree:


A skewed binary tree is a type of binary tree in which all the nodes have only either
one child or no child.
1. Left Skewed Binary Tree:

These are those skewed binary trees in which all the nodes are having a left child or
no child at all. It is a left side dominated tree. All the right child

2. Right Skewed Binary Tree:

These are those skewed binary trees in which all the nodes are having a right child
or no child at all. It is a right side dominated tree. All the left children remain as null.

Converting tree to binary tree:


Root of general tree =Root of binary tree

Left child of node in general tree=Left most child of binary tree

Right sibling of node in general tree=right most child of node binary tree
Binary Tree Traversals:
1. Depth First Search or DFS

▪In a depth first traversal all the nodes on a branch are visited before any others
are visited

In order Traversal - left root right


Preorder Traversal - root left right
Post order Traversal - left right root

Binary Search Tree:


A Binary Search Tree (BST) is a special type of binary tree in which the left child
of a node has a value less than the node’s value and the right child has a value
greater than the node’s value. This property is called the BST property and it
makes it possible to efficiently search, insert, and delete elements in the tree.

Binary Search Tree is a node-based binary tree data structure that has the following
properties:

The left subtree of a node contains only nodes with keys lesser than the node’s
key.
The right subtree of a node contains only nodes with keys greater than the node’s
key.
This means everything to the left of the root is less than the value of the root and
everything to the right of the root is greater than the value of the root. Due to
this performing, a binary search is very easy.
The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes(BST may have duplicate values with different
handling approaches)

You might also like