Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Tree Data Structure

The document discusses tree data structures, including their basic terminology, properties, operations, applications, and common types like binary trees, binary search trees, AVL trees, and B-trees. Key terms defined include root, child, parent, and leaf nodes as well as depth, height, and degree. Common operations are insertion, searching, and deletion.

Uploaded by

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

Tree Data Structure

The document discusses tree data structures, including their basic terminology, properties, operations, applications, and common types like binary trees, binary search trees, AVL trees, and B-trees. Key terms defined include root, child, parent, and leaf nodes as well as depth, height, and degree. Common operations are insertion, searching, and deletion.

Uploaded by

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

A tree data structure is a hierarchical structure that is used to represent and organize data in a way

that is easy to navigate and search. It is a collection of nodes that are connected by edges and has a
hierarchical relationship between the nodes.

The topmost node of the tree is called the root, and the nodes below it are called the child nodes.
Each node can have multiple child nodes, and these child nodes can also have their own child nodes,
forming a recursive structure.

Basic Terminologies In Tree Data Structure:

• Parent Node: The node which is a predecessor of a node is called the parent node of that
node. {B} is the parent node of {D, E}.
• Child Node: The node which is the immediate successor of a node is called the child node of
that node. Examples: {D, E} are the child nodes of {B}.
• Root Node: The topmost node of a tree or the node which does not have any parent node is
called the root node. {A} is the root node of the tree. A non-empty tree must contain exactly
one root node and exactly one path from the root to all other nodes of the tree.
• Leaf Node or External Node: The nodes which do not have any child nodes are called leaf
nodes. {K, L, M, N, O, P} are the leaf nodes of the tree.
• Ancestor of a Node: Any predecessor nodes on the path of the root to that node are called
Ancestors of that node. {A,B} are the ancestor nodes of the node {E}
• Descendant: Any successor node on the path from the leaf node to that node. {E,I} are the
descendants of the node {B}.
• Sibling: Children of the same parent node are called siblings. {D,E} are called siblings.
• Level of a node: The count of edges on the path from the root node to that node. The root
node has level 0.
• Internal node: A node with at least one child is called Internal Node.
• Neighbour of a Node: Parent or child nodes of that node are called neighbors of that node.
• Subtree: Any node of the tree along with its descendant.

Properties of Tree Data Structure:

• Number of edges: An edge can be defined as the connection between two nodes. If a tree
has N nodes then it will have (N-1) edges. There is only one path from each node to any
other node of the tree.
• Depth of a node: The depth of a node is defined as the length of the path from the root to
that node. Each edge adds 1 unit of length to the path. So, it can also be defined as the
number of edges in the path from the root of the tree to the node.
• Height of a node: The height of a node can be defined as the length of the longest path from
the node to a leaf node of the tree.
• Height of the Tree: The height of a tree is the length of the longest path from the root of the
tree to a leaf node of the tree.
• Degree of a Node: The total count of subtrees attached to that node is called the degree of
the node. The degree of a leaf node must be 0. The degree of a tree is the maximum degree
of a node among all the nodes in the tree.

Operations of Tree Data Structure

The basic operations that can be performed in a tree data structure include:

• Insertion: There are multiple ways to insert an element depending on the location, like
inserting the element in the rightmost or the leftmost vacant position or inserting the
element in the first empty position available.
• Searching: It is a simple process to search in a binary tree to check if the current node value
matches the required value. The process can be repeated to the right and left subtrees with
a recursive algorithm until a match is found.
• Deletion: Deleting is a tricky process in a tree data structure. When we delete a node,
complications may occur to the right and left children. A deleted node is a leaf node that
occurs. Thus, the deletion process of a node includes:
• Checking if the root is NULL.
• Searching for an item in the left and right subtree and recursing it.
• Deleting the root node of a tree.

Applications of a Tree Data Structure

The tree data structure stores the data in a hierarchical manner. The nodes are arranged at multiple
levels. Thus, the application of a tree data structure is:

• Information in the computer is stored hierarchically. There are multiple folders in a drive, and
each folder has many sub-folders. The files include images, documents etc.
• It is easy to search for a node in a tree data structure. Operations like insertion and deletion
are easy to perform.
• The tree data structure is used to store data in the routing tables.
• Programmers also use syntax trees to verify the syntax of the programs they write.

Types of Tree

• Binary Tree
• Binary Search Tree
• AVL Tree
• B-Tree

Binary Tree
A binary tree is a tree data structure in which each parent node can have at most two children. Each
node of a binary tree consists of three
items:

• data item
• address of left child
• address of right child

Types of Binary Tree

1. Full Binary Tree

A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.

2. Perfect Binary Tree

A perfect binary tree is a type of binary tree in which every internal


node has exactly two child nodes and all the leaf nodes are at the
same level.

3. Complete Binary Tree

A complete binary tree is just like a full binary tree, but with two major
differences

• Every level must be completely filled


• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a complete
binary tree doesn't have to be a full binary tree.

Binary Search Tree(BST)

Binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers.

• It is called a binary tree because each tree node has a maximum of two children.
• It is called a search tree because it can be used to search for the presence of a number in
O(log(n)) time.

The properties that separate a binary search tree from a regular binary tree is

• All nodes of left subtree are less than the root node
• All nodes of right subtree are more than the root node
• Both subtrees of each node are also BSTs i.e. they have the above two properties
Search Operation

Insert Operation
AVL Tree

To understand AVL Tree, we first need to understand the term - Balance Factor.

• The balance factor of a node is the difference between the height of the left subtree and the
height of the right subtree of that particular node.
• It can also be defined as a height-balanced binary search tree.
• For a tree to be height balanced, the balance factor of each node must be between -1 to 1 (-
1, 0, or 1).

B-tree

B-tree is a special type of self-balancing search tree in which each node can contain more than one
key and can have more than two children. It is a generalized form of the binary search tree.

It is also known as a height-balanced m-way tree.

A B tree of order m contains all the properties of an M way tree. In addition, it contains the following
properties.
• Every node in a B-Tree contains at most m children.
• Every node in a B-Tree except the root node and the leaf node contain at least m/2 children.
• The root nodes must have at least 2 nodes.
• All leaf nodes must be at the same level.

Operations

Searching :

Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49
in the following B Tree. The process will something like following :

• Compare item 49 with root node 78. since 49 < 78 hence, move to its left sub-tree.
• Since, 40<49<56, traverse right sub-tree of 40.
• 49>45, move to right. Compare 49.
• match found, return.
• Searching in a B tree depends upon the height of the tree. The search algorithm takes O(log
n) time to search any element in a B tree.

You might also like