Docsity

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

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 ≤ i ≤ n , 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 2i ≤ n. 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