0% found this document useful (0 votes)
18 views

Trees

This document discusses data structures and trees. It defines trees and binary trees, including their properties and common terminology. It describes tree traversal algorithms like depth-first search and breadth-first search. It also covers implementing and analyzing the performance of common tree operations and algorithms like calculating depth, height and drawing trees.

Uploaded by

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

Trees

This document discusses data structures and trees. It defines trees and binary trees, including their properties and common terminology. It describes tree traversal algorithms like depth-first search and breadth-first search. It also covers implementing and analyzing the performance of common tree operations and algorithms like calculating depth, height and drawing trees.

Uploaded by

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

Data Structures

Trees

Summary
Topics
general trees, definitions and properties
interface and implementation
tree traversal algorithms

depth and height


pre-order traversal
post-order traversal

binary trees

properties
interface
implementation

Trees
So far we have seen linear structures
linear: before and after relationship
lists, vectors, arrays, stacks, queues, etc
Non-linear structure: trees
probably the most fundamental structure in computing
hierarchical structure
Terminology: from family trees (genealogy)

Trees
store elements hierarchically
the top element: root
except the root, each element has a parent
each element has 0 or more children

root

Trees
Definition
A tree T is a set of nodes storing elements such that the nodes have a parent-child
relationship that satisfies the following

if T is not empty, T has a special tree called the root that has no parent
each node v of T different than the root has a unique parent node w; each node with parent w is
a child of w

Recursive definition
T is either empty
or consists of a node r (the root) and a possibly empty set of trees whose roots are the
children of r
Terminology
siblings: two nodes that have the same parent are called siblings
internal nodes

nodes that have children

external nodes or leaves

nodes that dont have children

ancestors
descendants

Trees

internal nodes

leaves

root

Trees

ancestors of u

Trees

descendants of u

Application of trees
Applications of trees
class hierarchy in Java
file system
storing hierarchies in organizations

Tree ADT
Whatever the implementation of a tree is, its interface is the following
root()
size()
isEmpty()
parent(v)
children(v)
isInternal(v)
isExternal(v)
isRoot()

Algorithms on trees
Definition:
depth(T, v) is the number of ancestors of v, excluding v itself

//compute the depth of a node v in tree T


int depth(T, v)
recursive formulation
if v == root, then depth(v) = 0
else, depth(v) is 1 + depth (parent(v))
Algorithm:
int depth(T,v) {
if T.isRoot(v) return 0
return 1 + depth(T, T.parent(v))
}

Analysis:
O(number of ancestors) = O(depth_v)
in the worst case the path is a linked-list and v is the leaf
==> O(n), where n is the number of nodes in the tree

Algorithms on trees
Definition:
height of a node v in T is the length of the longest path from v to any leaf

//compute the height of tree T


int height(T,v)

recursive definition:
if v is leaf, then its height is 0
else height(v) = 1 + maximum height of a child of v
definition:
the height of a tree is the height of its root
Proposition: the height of a tree T is the maximum depth of one of its leaves.

Height
Algorithm:
int height(T,v) {
if T.isExternal(v) return 0;
int h = 0;
for each child w of v in T do
h = max(h, height(T, w))
return h+1;
}

Analysis:
total time: the sum of times spent at each node, for all nodes
the algorithm is recursive;
v calls height(w) on all children w of v
height() will eventually be called on every descendant of v
is called on each node precisely once, because each node has one parent
aside from recursion
for each node v: go through all children of v
O(1 + c_v) where c_v is the number of children of v
over all nodes: O(n) + SUM (c_v)
each node is child of only one node, so its processed once as a child
SUM(c_v) = n - 1
total: O(n), where n is the number of nodes in the tree

Binary trees

Binary trees
Definition: A binary tree is a tree such that
every node has at most 2 children
each node is labeled as being either a left chilld or a right child

Recursive definition:
a binary tree is empty;
or it consists of
a node (the root) that stores an element
a binary tree, called the left subtree of T
a binary tree, called the right subtree of T

Binary tree interface


Tree T
left(v)
right(v)
hasLeft(v)
hasRight(v)
+ isInternal(v), is External(v), isRoot(v), size(), isEmpty()

Properties of binary trees


In a binary tree
level 0 has <=
level 1 has <=
level 2 has <=
...
level i has <=

1 node
2 nodes
4 nodes

d=0
d=1

2^i nodes

d=2

d=3

Proposition: Let T be a binary tree with n nodes and height h. Then


h+1 <= n <= 2 h+1 -1
lg(n+1) - 1 <= h <= n-1

Binary tree operations


insertLeft(v,e):
create and return a new node w storing element e, add w as the left child of v
an error occurs if v already has a left child
insertRight(v,e)

remove(v):
remove node v, replace it with its child, if any, and return the element stored at v
an error occurs if v has 2 children
addRoot(e):
create and return a new node r storing element e and make r the root of the tree;
an error occurs if the tree is not empty
attach(v,T1, T2):
attach T1 and T2 respectively as the left and right subtrees of the external node v
an error occurs if v is not external

Performance
all

O(1)
left(v)
right(v)
hasLeft(v)
hasRight(v)
isInternal(v)
is External(v)
isRoot(v)
size()
isEmpty()
addRoot(e)
insertLeft(v,e)
insertRight(v,e)
remove(e)

Application: Tree drawing


We can use an in-order traversal for drawing a tree. We can draw a binary tree by assigning
coordinate x and y of each node in the following way:
x(v) is the number of nodes visited before v in the in-order traversal of v
y(v) is the depth of v

0
1
2
3
4

You might also like