0% found this document useful (0 votes)
30 views

Introduction To Tree DS

The document discusses different types of tree data structures, including binary trees, ternary trees, n-ary trees, binary search trees, AVL trees, red-black trees, and B-trees. Binary trees can have two children per node, ternary trees have three children per node, and n-ary trees can have many children per node. Binary search trees, AVL trees, red-black trees, and B-trees are specialized types of trees that maintain certain balancing properties.

Uploaded by

ballu2711nov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Introduction To Tree DS

The document discusses different types of tree data structures, including binary trees, ternary trees, n-ary trees, binary search trees, AVL trees, red-black trees, and B-trees. Binary trees can have two children per node, ternary trees have three children per node, and n-ary trees can have many children per node. Binary search trees, AVL trees, red-black trees, and B-trees are specialized types of trees that maintain certain balancing properties.

Uploaded by

ballu2711nov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tree

What is a tree?

A tree is a kind of data structure that is used to represent the data in hierarchical
form. It can be defined as a collection of objects or entities called as nodes that are
linked together to simulate a hierarchy. Tree is a non-linear data structure as the
data in a tree is not stored linearly or sequentially.

Types of Trees based on the number of children:

Types of Trees in Data Structure based on the number of children

1. Binary Tree
A binary Tree is defined as a Tree data structure with at most 2 children. Since
each element in a binary tree can have only 2 children, we typically name them
the left and right child.
Example:
Consider the tree below. Since each node of this tree has only 2 children, it can
be said that this tree is a Binary Tree
Types of Binary Tree:
Binary Tree consists of following types based on the number of children:
a. Full Binary Tree
b. Almost complete Binary Tree
c. Complete Binary Tree
d. Binary Search Tree

2. Ternary Tree
A Ternary Tree is a tree data structure in which each node has at most three child
nodes, usually distinguished as “left”, “mid” and “right”.
Example:
Consider the tree below. Since each node of this tree has only 3 children, it can
be said that this tree is a Ternary Tree

Ternary Tree

3. N-ary Tree (Generic Tree)


Generic trees are a collection of nodes where each node is a data structure that
consists of records and a list of references to its children(duplicate references are
not allowed). Unlike the linked list, each node stores the address of multiple
nodes.
Every node stores the addresses of its children and the very first node’s address
will be stored in a separate pointer called root.
1. Many children at every node.
2. The number of nodes for each node is not known in advance.
Example:
Special Types of Trees in Data Structure based on the nodes’ values:

1. Binary Search Tree


A 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.
 The left and right subtree each must also be a binary search tree.

Binary Search Tree

2. AVL Tree
AVL tree is a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees for any node cannot be more than one.
AVL Tree

3. Red-Black Tree
A red-black tree is a kind of self-balancing binary search tree where each node
has an extra bit, and that bit is often interpreted as the color (red or black). These
colors are used to ensure that the tree remains balanced during insertions and
deletions.
Although the balance of the tree is not perfect, it is good enough to reduce the
searching time and maintain it around O(log n) time, where n is the total number
of elements in the tree.
Rules That Every Red-Black Tree Follows:
1. Every node has a color either red or black.
2. The root of the tree is always black.
3. There are no two adjacent red nodes (A red node cannot have a red parent
or red child).
4. Every path from a node (including root) to any of its descendants’ NULL
nodes has the same number of black nodes.
5. All leaf (NULL) nodes are black nodes.

Red-Black Tree

4. B-Tree
B-Tree is a self-balancing search tree. In most of the other self-balancing search
trees (like AVL and Red-Black Trees), it is assumed that everything is in the main
memory.
Properties of B-Tree:
 All leaves are at the same level.
 B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘ depends
upon disk block size.
 Every node except the root must contain at least t-1 keys. The root may
contain a minimum of 1 key.
 All nodes (including root) may contain at most (2*t – 1) keys.
 The number of children of a node is equal to the number of keys in it plus 1.
 All keys of a node are sorted in increasing order. The child between two
keys k1 and k2 contains all keys in the range from k1 and k2.
 B-Tree grows and shrinks from the root which is unlike Binary Search Tree.
Binary Search Trees grow downward and also shrink from downward.
 Like other balanced Binary Search Trees, the time complexity to search, insert
and delete is O(log n).
 Insertion of a Node in B-Tree happens only at Leaf Node.

You might also like