Trees in data Structure, Lecture notes of Data Structures and Algorithms

Tree,Binary tree,Node,Linked Representation

Typology: Lecture notes

2018/2019

Uploaded on 06/26/2019

monaa_pin
monaa_pin 🇮🇳

4 documents

1 / 110

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Trees
Definition: 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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Trees in data Structure and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Trees

• Definition: 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 T 1 , …, Tn, where

each of these sets is a tree. We call T 1 ,

…, Tn the subtrees of the root.

Pedigree Genealogical

Chart

Chery l Kevin Rosemar y John Terry Richard^ Kelly Jack (^) Mary Anthon y Karen Joe Michelle^ Mike Angela Binary Tree

Tree Terminology

• Normally we draw a tree with the root at the

top.

  • (^) The degree of a node is the number of

subtrees of the node.

  • (^) The degree of a tree is the maximum degree of

the nodes in the tree.

  • (^) A node with degree zero is a leaf or terminal

node.

  • (^) A node that has subtrees is the parent of the

roots of the subtrees, and the roots of the

subtrees are the children of the node.

  • (^) Children of the same parents are called

siblings.

Tree Terminology

(Cont.)

• The ancestors of a node are all the

nodes along the path from the root to the

node.

• The descendants of a node are all the

nodes that are in its subtrees.

• Assume the root is at level 1, then the

level of a node is the level of the node’s

parent plus one.

• The height or the depth of a tree is the

maximum level of any node in the tree.

List Representation of

Trees

A
B F 0 C G 0
D I J 0
E K L 0 H M 0

Possible Node Structure

For A Tree of Degree

  • (^) Lemma 5.1: If T is a k-ary tree (i.e., a tree of degree k) with n nodes, each having a fixed size as in Figure 5.4, then n(k-1) + 1 of the nk child fileds are 0, n ≥ 1. Data Child 1 Child 2 Child 3 Child 4 …^ Child k Wasting memory!

Degree Two Tree

Representation

A
B
C
D
E
F G
H
I
J
K
L
M

Binary Tree!

Binary Tree

• Definition: A binary tree is a finite set of

nodes that is either empty or consists of

a root and two disjoint binary trees

called the left subtree and the right

subtree.

• There is no tree with zero nodes. But

there is an empty binary tree.

• Binary tree distinguishes between the

order of the children while in a tree we

do not.

Binary Tree Examples

A
B
A
B
A
B
C
D
E
A
B C
D E F G
H I

The Properties of Binary

Trees

  • (^) Lemma 5.2 [Maximum number of nodes]

1) The maximum number of nodes on level i of a

binary tree is 2 i-1, i ≥ 1.

2) The maximum number of nodes in a binary tree of

depth k is 2 k^ – 1, k ≥ 1.

  • (^) Lemma 5.3 [Relation between number of

leaf nodes and nodes of degree 2]: For any

non-empty binary tree, T, if n 0 is the number

of leaf nodes and n2 the number of nodes of

degree 2, then n 0 = n 2 + 1.

  • (^) Definition : A full binary tree of depth k is

a binary tree of depth k having 2 k^ – 1 nodes,

k ≥ 0.

Binary Tree Definition

  • (^) Definition : A binary tree with n nodes and

depth k is complete iff its nodes correspond to

the nodes numbered from 1 to n in the full

binary tree of depth k.

leve l (^1) 2 3 4

Array Representation of A

Binary Tree

If a complete binary tree with n nodes is represented sequentially, then for any node with index i, 1 ≤ in , we have:

  • (^) parent( i ) is at if i ≠1. If i = 1, i is at the

root and has no parent.

  • left_child( i ) is at 2 i if 2in. If 2i > n, then i has no

left child.

  • (^) right_child( i ) is at 2 i + 1 if 2 i + 1 ≤ n. If 2 i + 1 > n ,

then i has no right child.

i /^2 

Linked Representation

class Tree; class TreeNode { friend class Tree; private : TreeNode *LeftChild; char data; TreeNode *RightChild; }; class Tree { public : // Tree operations . private : TreeNode *root; };

Node Representation

LeftChild data RightChild data LeftChild RightChil d