CPCS 204: Data Structures - PowerPoint PPT Presentation

About This Presentation
Title:

CPCS 204: Data Structures

Description:

CPCS 204: Data Structures & Algorithms Chapter 7 Trees Instructor: Nouf Ghanimi – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 21
Provided by: root106
Category:
Tags: cpcs | data | ponzi | structures

less

Transcript and Presenter's Notes

Title: CPCS 204: Data Structures


1
CPCS 204 Data Structures AlgorithmsChapter 7
Trees
  • Instructor Nouf Ghanimi

2
overview
  • 7.1 General Trees
  • 7.2  Tree Traversal Algorithms

3
Trees
  • non-linear Data Structures
  • Tree is a set of nodes and edges that connect
    them.
  • Between each node there exactly one path between
    nodes.
  • A path is connected sequence of edges.

4
Rooted tree
  • Rooted tree one distinguished node is called the
    root.
  • Every node c, except the root, has one parent
    called p. where p is the first node on the path
    from c to the root.
  • Since p is the parent of c then c is ps child

5
Rooted tree
  • Root has no parent.
  • A node can have any of children.
  • Example.

6
Rooted tree
  • Leaf node with no children (external), opposite
    is internal.
  • Sibling nodes with same parent.
  • Ancestor of node d nodes on the path from d to
    the root, including d, ds parent , ds grand
    parent, , root.
  • If a is ancestor of d , then d is descendant of
    a.

7
Rooted tree
  • Length of path edges in path (Example).
  • Depth of node n length of path from n to root,
    ex depth of root is zero.
  • Height of node n length of path from n to its
    deepest descendant.

8
Rooted tree
  • Ex height of any leaf is zeroheight of tree
    height of the root
  • Sub tree rooted at n tree formed by n its
    descendants.
  • Binary tree no node has gt 2 children
  • Every child is a left or right child, even if
    its the only child.

9
Web page example
10
Representing rooted trees
  • Each node has three references
  • Item
  • Parent
  • Children stored in a list

11
Tree Traversal
  • A traversal is a manner of visiting each node in
    a tree once.

12
1) Preorder traversal
  • Visit each node before recursively visiting its
    children, left to right. Root is visited first.
  • Each node visited only once.
  • It takes O(n) time , where n is of nodes in
    tree.
  • It is natural way to print the structure of
    directories

13
1) Preorder Traversal
Algorithm preOrder(v) visit(v) for each child w
of v preOrder (w)
1
Make Money Fast!
2
5
9
1. Motivations
References
2. Methods
6
7
8
3
4
2.1 StockFraud
2.2 PonziScheme
2.3 BankRobbery
1.1 Greed
1.2 Avidity
14
2) Postorder traversal
  • Visited each node children ,left to right, before
    the node itself.
  • Natural way to sum the disk space in directories.

15
Postorder Traversal
Algorithm postOrder(v) for each child w of
v postOrder (w) visit(v)
9
cs16/
8
3
7
todo.txt1K
homeworks/
programs/
4
6
5
1
2
DDR.java10K
Stocks.java25K
h1c.doc3K
h1nc.doc2K
Robot.java20K
16
3) In order traversal
  • This is only with binary tree.
  • Visit left child, then visit the node itself,
    then you visit the right child.

17
Inorder Traversal
Algorithm inOrder(v) if hasLeft (v) inOrder (left
(v)) visit(v) if hasRight (v) inOrder (right (v))
6
2
8
1
7
9
4
3
5
18
Expression tree
  • You can generate a formula of expression called
    postfix, prefix, infix expressions.
  • In order 3 7 4 2
  • Preorder 3 7 4 2
  • Postorder 3 7 4 2

19
4) Level-order traversal
  • Visit the root, then visit all depth-1 nodes
    (left to right), then all depth -2 nodes, etc.
  • 3 7 4 2 (means nothing)

20
4) Level-order traversal
  • Implementation not recursive.
  • Use a queue which initially contains only the
    root.
  • Repeat, dequeue a node from the list
  • , visit it
  • ,enqueue its children (left to right)
  • Until queue is empty
Write a Comment
User Comments (0)
About PowerShow.com