Trees
This presentation is the property of its rightful owner.
Sponsored Links
1 / 13

Trees PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Trees. Definitions Read Weiss, 4.1 – 4.2 Implementation Nodes and Links One Arrays Three Arrays Traversals Preorder, Inorder, Postorder K-ary Trees Converting trees: k-ary ↔ binary. Nodes Edges Root node Interior node Leaf Parent Children. Ancestor / Proper ancestor

Download Presentation

Trees

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Trees

Trees

  • Definitions

  • Read Weiss, 4.1 – 4.2

  • Implementation

  • Nodes and Links

  • One Arrays

  • Three Arrays

  • Traversals

  • Preorder, Inorder, Postorder

  • K-ary Trees

  • Converting trees: k-ary↔ binary


Definitions

Nodes

Edges

Root node

Interior node

Leaf

Parent

Children

Ancestor / Proper ancestor

Descendant / Proper descendant

Level of a node

Height of a node / tree

Degree of a node / tree

Depth of node

Path

Acyclic graph

Definitions


Trees

descendants of “a”

a

root, height=4, depth=level=0

degree=1

proper descendants of “a”

j

b

k

c

interior node, height=2, depth=level=2

degree=2

g

m

d

l

i

h

f

e

degree=0

leaf, height=0, depth=level=4

degree of tree = 2

height of tree = 4


Trees

Implementing a Tree

  • Nodes and Links

Node {

Object value;

Node lchild;

Node rchild;

} // Node

A

B

C

D

▲=null link


Trees

Implementing a Tree

A: 0 1 2 3 4 5 6 7 8 9

- A B C - - D - - -

  • One array

  • A[1] is root

  • lchild of A[i] is A[2i]

  • rchild of A[i] is A[2i+1]

  • “-” means array element

  • is null / not used

  • A[0] not used as a node

  • A[0] may be used to hold

  • general info (e.g.,

  • number of nodes in tree)

A

B

C

D

▲=null link


Trees

A

B

C

D

Implementing a Tree

  • Three array

▲=null link

root = 4

freelist = 7

null = “/” = -1

0 1 2 3 4 5 6 7 8 9

Object value: D - - - A - B - C -

int lchild: / / 3 5 6 1 / 9 0 2

int rchild: / / / / 8 / / / / /


Traversals

Traversals

  • Preorder

    N L R

preorder (Node t)

if (t == null)

return;

visit (t.value());

preorder (t.lchild());

preorder (t.rchild());

} // preorder


Trees

Traversals

  • Inorder

    L N R

inorder (Node t)

if (t == null)

return;

inorder (t.lchild());

visit (t.value());

inorder (t.rchild());

} // inorder


Trees

Traversals

  • Postorder

    L R N

postorder (Node t)

if (t == null)

return;

postorder (t.lchild());

postorder (t.rchild());

visit (t.value());

} // postorder


Trees

a

j

b

k

c

g

m

d

l

i

h

f

e

preorder: a j k m l b c g i h d f e

inorder: m k l j a b i g h c f d e

postorder: m l k j i h g f e d c b a


K ary trees

K-ary Trees

a

q

c

e

f

b

d

n

m

i

p

g

k

j

degree of tree = 4

degree of nodes f and n = 3

height of tree = 3

depth=level of m = 2


Trees

K-ary Tree => Binary Tree

a

q

c

e

f

b

d

n

m

i

p

K-aryBinary

rootroot

leftmost childleft child

right siblingright child

g

k

j


Traversals1

a

q

c

e

f

a

b

d

q

c

e

f

n

m

i

p

b

d

n

m

i

p

g

k

j

g

k

j

Traversals

K-ary Tree

Binary Tree

Preorder:

Inorder:

Postorder:

Preorder:

Inorder:

Postorder:


  • Login