You are on page 1of 21

Definition of Tree

A tree is a finite set of one or more nodes


such that:
There is a specially designated node called
the root.
The remaining nodes are partitioned into n>=0
disjoint sets T1, ..., Tn, where each of these sets is
a tree.
We call T1, ..., Tn the subtrees of the root.
For n nodes we have exactly n-1 edges
Prof. Gayathri. P, SCSE, VIT University

Representation
ROOT OF
TREE T

T1

T2

T3

T4

T5

SUBTREES
Prof. Gayathri. P, SCSE, VIT University

Prof. Gayathri. P, SCSE, VIT University

Prof. Gayathri. P, SCSE, VIT University

Binary Trees
A binary tree is a tree in which no node can
have more than two subtrees i.e atmost 2
children.
In other words, a node can have zero, one, or
two subtrees.
These subtrees are designated as the left
subtree and the right subtree.

Prof. Gayathri. P, SCSE, VIT University

Binary Trees: General Representation


ROOT OF
TREE T

T1

T2

SUBTREES
Prof. Gayathri. P, SCSE, VIT University

Prof. Gayathri. P, SCSE, VIT University

Strictly Binary Tree


Right-Skewed Binary Tree
Left-Skewed Binary Tree

Full Binary Tree

Complete Binary Tree


Prof. Gayathri. P, SCSE, VIT University

Binary Tree Representation


Array representation.
Linked representation.

Prof. Gayathri. P, SCSE, VIT University

Array Representation
Root is stored at index 1 of the array.
Left child at 2i and right child at 2i+1 where I is the index of the
parent.
If array index begins with 0, then root at 0, left child at 2i+1 and right
child at 2i+2 where i is the index of the parent.

a1

b
4

c
5

8
h

tree[]

7
g

10
j

a b c d e f g h i j
0
5
10
Prof. Gayathri. P, SCSE, VIT University

10

Linked List
Struct BinaryTreeNode
{

int element;
struct BinaryTreeNode *leftChild; // pointer to left
subtree

struct BinaryTreeNode *rightChild; // pointer to right


subtree
} * Btnode;

Prof. Gayathri. P, SCSE, VIT University

11

Linked Representation Example


root

a
b

c
e

# - Null

leftChild
element
rightChild
Prof. Gayathri. P, SCSE, VIT University

g
#

#
12

BT Traversal
Refer PDF file
Also Refer C program document
Fourth type of traversal is Level order
Visit the nodes from top level to bottom level. In each level,
visit nodes from left to right.
Level order traversal for the example tree is
7 1 9 0 3 8 10 2 5 4 6

Prof. Gayathri. P, SCSE, VIT University

13

Terminology - Definition
Number of nodes = 9
The degree of a node is the number of sub trees of the node
The degree of A is 3; the degree of C is 0; the degree of B is 2 .
The node with degree 0 or node without any children is a leaf or terminal
node.
The node with children is a non-terminal node. B, F, A
A node that is not a root or a leaf is known as an internal node.
All non-terminal nodes are parents and they have subtrees.
The roots of these subtrees are the children of the parent node.
Children of the same parent are siblings.

Degree of a tree = 3 (maximum degree of any of its nodes).


The level of a node is its distance from the root. The root is at level 0, its children are at
level 1, etc.
Prof. Gayathri. P, SCSE, VIT University

14

Path - A path from node n1 to node nk is a sequence of nodes n1, n2,....., nk

Path Length - The length of path is the number of edges on the path
There is a path of length zero from every node to itself.
In a tree there is one and only one path from the root to each node.

Depth of a node - The depth of a node is the length of the path from
the root to the node. The root is at depth 0.

Depth of the tree - The depth of a tree is the depth of its deepest leaf.

Height of a node - The height of any node is the longest path from the
node to any leaf. The height of any leaf is 0.

Height of the tree - The height of a tree is the height of its root.

The height and depth of a tree are equal.


Prof. Gayathri. P, SCSE, VIT University

15

Representation of General Tree


Linked list representation

data

link 1

link 2

...

link n
How many link fields are
needed in such a representation?

Prof. Gayathri. P, SCSE, VIT University

16

A Tree Node
Every tree node:
object useful information
children pointers to its children nodes
O

O
Prof. Gayathri. P, SCSE, VIT University

17

Left Child - Right Sibling


A
B
E
K

C
F

D
H

data

right sibling

left child

Prof. Gayathri. P, SCSE, VIT University

18

Tree Implementation
struct tnode {
int key;
struct tnode* lchild;
struct tnode* sibling;
} *ptnode;

Prof. Gayathri. P, SCSE, VIT University

19

Converting to a Binary Tree


A

Binary tree left child


= leftmost child
Binary tree right child
= right sibling

B
C
D

A
F
B

E
G

J
I
General Trees

J
20

For self study


Implementation of Doubly linked list, circular
singly linked list, circular doubly linked list
Implementation of polynomial addition,
subtraction, multiplication
Implementation of general tree to binary tree
conversion
Implementation of linked stack and linked
queue
Priority queues and circular queues
Prof. Gayathri. P, SCSE, VIT University

21

You might also like