TREE ADT - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

TREE ADT

Description:

Directory Structure represented by a Tree. XU Department of Computer Science. ics20:s10:4 ... Proper BT: 0 or 2 children. recursive definition: BT is either: an ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 48
Provided by: jayrcfer
Category:
Tags: adt | tree | bt | directory

less

Transcript and Presenter's Notes

Title: TREE ADT


1
TREE ADT
  • Department of Computer Science
  • Xavier University

2
Trees
  • collection of elements called nodes, one of which
    is distinguished as a root, along with a relation
    (parenthood)
  • imposes a hierarchical structure on a collection
    of items
  • e.g. organizational charts

3
Trees
C\
CS
MATH
CS20
CS22
MA5
MA6
Directory Structure represented by a Tree
4
Trees
  • a tree T is a set of nodes storing elements in a
    parent-child relationship with the following
    properties
  • T has a distinguished node r, called the root of
    T, that has no parent
  • each node v of T distinct from r has a parent
    node u
  • for every node v of T, the root r is an ancestor
    of v

5
Trees
  • a single node by itself is a tree (this node is
    also the root)

A
6
Trees
  • an ancestor of a node is either the node itself
    or an ancestor of the parent of the node
  • a node v is a descendent of a node u if u is an
    ancestor of v

7
Trees
A is an ancestor of E E is a descendant of A
8
Trees
  • if node u is the parent of node v, then we say
    that v is a child of u
  • two nodes that are children of the same parent
    are siblings

9
Trees
B and C are children of A B and C are siblings
10
Trees
  • a node is external if it has no children
  • a node is internal if it has one or more children
  • external nodes are also called leaves

11
Trees
A and B are internal nodes D, E, and C external
nodes
12
Trees
  • The subtree of T rooted at node v is the tree
    consisting of all the descendants of v in T
    (including v itself)

13
Trees
T2 is a subtree of T1 rooted at B
14
Trees
  • a tree is ordered if there is a linear ordering
    defined for the children of each node in such a
    way that we can identify children of a node as
    being the first, second, third, and soon

15
Trees
Book
Part 1
Part 2
Sec 1.1
Sec 1.2
Sec 2.1
Sec 2.2
Book Structure represented by an Ordered Tree
16
Trees
  • Binary Tree
  • an ordered tree with each internal nodes having
    at most 2 children.
  • Proper BT 0 or 2 children
  • recursive definition BT is either
  • an external node (leaf) or
  • an internal node (the root) and two binary trees
    (left subtree and right subtree)

17
Trees
  • Binary Trees Properties
  • ( external nodes ) ( internal nodes) 1
  • ( nodes at level i) ? 2i
  • ( external nodes) ? 2(height)
  • (height) log2 ( external nodes)
  • (height) log2 ( nodes) 1
  • (height) ? ( internal nodes) (( nodes) - 1)/2

18
Trees
  • Binary Trees

Level
A
19
Trees
20
Trees
  • the depth of v is the number of ancestors of v,
    excluding v itself
  • the depth of a root is 0

21
Trees
22
Trees
  • the height of a node v is
  • if v is an external node, the height of v is 0
  • otherwise, the height of v is one plus the
    maximum height of a child of v
  • the height of an entire tree is height of the
    root
  • height maximum depth of a node

23
Trees
The height of B is 1 The height of the entire
tree is 2
24
Trees
  • Tree ADT Methods
  • generic container methods
  • size(), isEmpty(), elements()
  • positional container methods
  • positions(), swapElements(p,q),
    replaceElement(p,e)

25
Trees
  • Tree ADT Methods
  • query methods
  • isRoot(p), isInternal(p), isExternal(p)
  • accessor methods
  • root(), parent(p), children(p)

26
Trees
  • Binary Tree ADT Methods
  • accessor methods
  • leftChild(p), rightChild(p), sibling(p)
  • update methods
  • expandExternal(p), removeAboveExternal(p)

27
Trees
  • Tree Traversals
  • a systematic way of accessing or visiting all the
    nodes of T
  • basic traversal schemes
  • Preorder Traversal
  • Postorder Traversal
  • Inorder Traversal

28
Trees
  • Preorder Traversal
  • the root of T is visited first and then the
    subtrees rooted at its children are traversed
    recursively

29
Trees
A B D E C
30
Trees
  • Algorithm preorder(T, v)
  • perform the visit action for node v
  • for each child w of v do
  • recursively traverse the subtree rooted at w
    by calling preorder(T,w)

31
Trees
  • Preorder Traversal
  • if the tree is ordered, then the subtrees are
    traversed according to the order of the children

32
Trees
  • Postorder Traversal
  • opposite of the preorder traversal because it
    recursively traverses the subtrees rooted at the
    children of the root first, and then visits the
    root

33
Trees
D E B C A
34
Trees
  • Algorithm postorder(T, v)
  • for each child w of v do
  • recursively traverse the subtree rooted at w
    by calling preorder(T,w)
  • perform the visit action for node v

35
Trees
  • Inorder Traversal
  • visit a node between the recursive traversals of
    its left and right subtrees

36
Trees
D B E A C
37
Trees
  • Algorithm inorder(T, v)
  • if v is an internal node then
  • inorder(T,T.leftChild(v))
  • perform the visit action for node v
  • if v is an internal node then
  • inorder(T,T.rightChild(v))

38
Trees
  • Sequence-Based Structure for B.T.
  • based on a certain way of numbering the nodes of
    T
  • node v of T is associated with the element of
    sequence S at rank p(v)
  • typically, implemented using an array

39
Trees
1
2
3
4
5
6
7
8
9
Binary Tree General Scheme
40
Trees
41
Trees
  • Sequence-Based Structure for B.T.
  • for every node v of T, let p(v) be the integer
    defined
  • if v is the root of T, then p(v)1
  • if v is the left child of node u, then p(v)2p(u)
  • if v is the right child of node u, then
    p(v)2p(u)1

42
Trees
  • Sequence-Based Structure for B.T.
  • function p is known as a level numbering of the
    nodes in a binary tree

43
Trees
  • Linked Structure for Binary Trees
  • each node v of T is represented by an object with
    references to the element stored at v and to the
    objects associated with the children and parent
    of v

44
Trees
parent
Left Child
Right Child
element
45
Trees
Ø
5
Root
Size
Part 1
Ø
Ø
Chap. 2
Chap. 1
Ø
Ø
Ø
Ø
Section 1.2
Section 1.1
46
Trees
parent
element
Children Container
47
Trees
Ø
4
Root
Size
Chap. 1
Sec. 1.1
Sec. 1.2
Sec. 1.3
Ø
Ø
Ø
Write a Comment
User Comments (0)
About PowerShow.com