Data Structures and Algorithms: Tree Concepts and Implementations, Slides of Data Structures and Algorithms

An in-depth exploration of tree concepts, including definitions, terminology, and tree adt methods. It also covers tree implementation using a struct in c and tree traversal methods such as preorder and postorder. The document also includes examples and explanations of binary trees.

Typology: Slides

2012/2013

Uploaded on 04/27/2013

shareeka_555
shareeka_555 🇮🇳

4

(6)

74 documents

1 / 46

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures &
Algorithm Analysis
Docsity.com
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

Partial preview of the text

Download Data Structures and Algorithms: Tree Concepts and Implementations and more Slides Data Structures and Algorithms in PDF only on Docsity!

Data Structures &

Algorithm Analysis

The British Constitution

Crown

Church of

England Cabinet^

House of Commons

House of Lords

Supreme Court

Ministers County Council

Metropolitan police County Borough Council

Rural District Council Docsity.com

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>=

disjoint sets T1, ..., Tn , where each of these sets is

a tree.

We call T1, ..., Tn the subtrees of the root.

Level and Depth

K L

E F

B

G

C

M

H I J

D

A

Level

1

2

3

4

node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4)

Tree Properties

A

B C

D

G

E F

H I

Property Value Number of nodes Height Root Node Leaves Interior nodes Number of levels Ancestors of H Descendants of B Siblings of E Right subtree

Representation of Trees

List Representation

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

The root comes first, followed by a list of sub-trees

data link 1 link 2 ... link n

How many link fields are needed in such a representation?

Left Child - Right Sibling

A

B C^ D

E F^ G^ H^ I^ J

K L^ M

data left child right sibling

Tree ADT

  • Objects: any type of objects can be stored in a tree
  • Methods:
  • accessor methods
    • root() – return the root of the tree
    • parent(p) – return the parent of a node
    • children(p) – returns the children of a node
  • query methods
    • size() – returns the number of nodes in the tree
    • isEmpty() - returns true if the tree is empty
    • elements() – returns all elements
    • isRoot(p), isInternal(p), isExternal(p)

Tree Traversal

• Two main methods:

  • Preorder
  • Postorder

• Recursive definition

• PREorder:

  • visit the root
  • traverse in preorder the children (subtrees)

• POSTorder

  • traverse in postorder the children (subtrees)

Preorder

  • preorder traversal Algorithm preOrder(v) “visit” node v for each child w of v do recursively perform preOrder(w)

Preorder Implementation

public void preorder(ptnode t) {

ptnode ptr; display(t->key); for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) { preorder(ptr); } }

Postorder Implementation

public void postorder(ptnode t) {

ptnode ptr; for(ptr = t->lchild; NULL != ptr; ptr = ptr->sibling) { postorder(ptr); } display(t->key); }

Example

J

M I

L^ H

A

B

C

D

E

K^ F^ G

ADT Binary Tree

objects: a finite set of nodes either empty or

consisting of a root node, left BinaryTree ,

and right BinaryTree.

method:

for all bt , bt1 , bt2 ∈ BinTree , item ∈ element

Bintree create()::= creates an empty binary tree

Boolean isEmpty( bt )::= if ( bt ==empty binary tree) return TRUE else return FALSE