0% found this document useful (0 votes)
9 views11 pages

09 Tree

Tree

Uploaded by

Neha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views11 pages

09 Tree

Tree

Uploaded by

Neha Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Tree

INTRODUCTION

• A tree is recursively defined as a set of one or more nodes where


one node is designated as the root of the tree and all the remaining
nodes can be partitioned into non-empty sets each of which is a
sub-tree of the root.

Figure shows a tree where


node A is the root node;
nodes B, C, and D are children of the
root node and form sub-trees of the
tree rooted at node A.
• Root: node without parent (A)
Tree Terminology
• Siblings: nodes share the same parent

• Internal node: node with at least one child (A, B, C, F)


A
• External node (leaf ): node without children (E, I, J, K, G, H,
D)

• Ancestors of a node: parent, grandparent, grand-grandparent,


etc.
B C D
• Descendant of a node: child, grandchild, grand-grandchild, etc.

• Depth of a node: number of ancestors

• Height of a tree: maximum depth of any node (3)


E F G H
• Degree of a node: the number of its children
The leaf of the tree does not have any child so its degree is zero

• Degree of a tree: the maximum degree of a node in the tree.

• Subtree: tree consisting of a node and its descendants


I J K
• Empty (Null)-tree: a tree without any node.

• Root-tree: a tree with only one node


Binary Trees

It is a data structure that is defined as a collection of elements called nodes.


In a binary tree,
• the topmost element is called the root node, and
• Each node has 0, 1, or at the most 2 children.
• A node that has zero children is called a leaf node or a terminal node.
• Every node contains a data element, a left pointer which points to the
left child, and a right pointer which points to the right child.
Complete Binary Trees

A complete binary tree is a binary tree that satisfies two


properties.

• First, in a complete binary tree, every level, except


possibly the last, is completely filled.
• Second, all nodes appear as far left as possible.
Representation of Binary Trees in the Memory
Linked representation of binary trees

In the linked representation, every node will have three parts:

the data element, a pointer to the left node, and a pointer to the right
node.

So in C++, the binary tree is built with a node type given below.

class node {
struct node *left;
int data;
struct node *right;
};

Every binary tree has a pointer ROOT, which points to the root element (topmost
element) of the tree. If ROOT = NULL, then the tree is empty.
Linked representation of binary trees
Sequential representation of binary tree

Sequential representation of trees is done using single or one-dimensional arrays. Though it


is the simplest technique for memory representation, it is inefficient as it requires a lot of
memory space.

A sequential binary tree follows the following rules:


1. A one-dimensional array, called TREE, is used to store the elements of tree.

2. The root of the tree will be stored in the first location. That is, TREE[1] will store the
data of the root element.

3. The children of a node stored in location K will be stored in locations (2 × K) and (2 ×


K+1).

4. The maximum size of the array TREE is given as (2 h+1–1), where h is the height of the
tree.

5. An empty tree or sub-tree is specified using NULL. If TREE[1] = NULL, then the tree
is empty.
Array Implementation of a binary tree
For any node k

The left child of a node K =2×K

The right child of a node K = 2 × K+1

Parent of any node K = floor (K/2)


Expression Tree

Binary trees are widely used to store algebraic expressions.

For example, consider the algebraic expression given as:


Exp = (a – b) + (c * d)
Given an expression,
Exp = ((a + b) – (c * d)) % ((f ^g) / (h – i)),

construct the corresponding binary tree.

You might also like