Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

... of parents and children we view it as parent, leftmost ... Were we to print out the node data in the first visit to each node the printout would be : ABC ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 20
Provided by: dickst
Category:
Tags: abc | the | trees | view

less

Transcript and Presenter's Notes

Title: Trees


1
Trees
  • CS-240
  • Dick Steflik

2
What is a 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 ngt0
    disjoint sets T1,..,Tn, where each of these sets
    is a tree. T1,..,Tn are the subtrees of the root.

3
Examples
4
Tree Terms
Root
A
A is the parent of B and C
B and C are children of A
B
C
height (h)
B and C are siblings
interior nodes
F
E
D
G
H
leaf nodes (exterior nodes)
5
height - the number of nodes in the longest path
going from the root to the furthest leaf parent -
any node in the tree at the next higher level in
the tree child - any node in the tree at the next
lower level in the tree siblings - any nodes in
the tree having a common parent order - the
number of children in the node having the largest
number of children binary tree - any order 2
tree binary search tree - any binary tree having
the search tree property
6
Nodal Structure Options
If we know the maximum order that an arbitrary
tree is supposed to be we could allocate our data
content and a child pointer for each possible
child
Ex suppose max. order 5
each node would look like
child 1
child 4
child 5
child 2
child 3
Data
If our tree has many nodes that have less than 5
children this representation could be very
wasteful considering that each child pointer
requires 4 bytes of storage. Is there a
better, less wasteful representation?
7
As it turns out, YES there is
The lowly order 2 (binary) tree can be used to
represent any order n tree. and we can make the
statement that
For any general tree, there is an equivalent
binary tree
To do this we must visualize an order 2 tree
differently instead of as a collection of
parents and children we view it as parent,
leftmost child and that childs siblings
Instead of this
This
A
A
B
C
B
C
D
D
8
Why do we want to do this?
It turns out that order 2 tree have a very nice
structure in that there are only two choices
to make Right or Left. This makes it easier to
design algorithms for them.
To explore this let us look at the problem of
creating an algorithm for visiting every node in
a tree in some predictable order. This problem
is called Traversal and can be accomplished with
the following the following algorithm. 1. start
at the root and 2. follow all of the left links
until you cant go any farther 3. back-up one
node and try going right, if you can repeat steps
2 and 3, if you cant repeat step 3
9
Traversal
Notice that each node is visited 3 times
Were we to print out the node data in the first
visit to each node the printout would be ABC
A
3
1
Were we to printout the node data on the second
visit to each node the printout would be BAC
2
1
Were we to printout the node data on the third
visit to each node the printout would be BCA
3
3
C
B
1
These are called the preorder, inorder and
postorder traversals respevtively
2
2
10
Pre-order Traversal
template lttypename Tgt void preorder( tnodeltTgt
t) if (t ! NULL) cout ltlt t -gt
data ltlt preorder(t -gt left)
preorder(t -gt right)
11
In-order Traversal
template lttypename Tgt void inorder( tnodeltTgt
t) if (t ! NULL) inorder(t -gt
left) cout ltlt t -gt data ltlt
inorder(t -gt right
12
Post-order Traversal
template lttypename Tgt void postorder( tnodeltTgt
t) if (t ! NULL)
postorder(t -gt left) postorder(t -gt
right) cout ltlt t -gt data ltlt

13
Notice that...
the first node visited on the pre-order traversal
ia always the root the left-most node in the
tree is the first node on the inorder
traversal the last node visited on the inorder
traversal is the rightmost node in the tree the
last node visited on the postorder traversal is
the root Knowing this and given any two
traversal paths the tree can be constructed.
14
Armed with this information
  • We should be able to construct the tree that
    produced any pair of traversal paths

15
Binary Search Trees
  • Order two tree
  • Each parent can have only two children
  • Key of left child is lt key of parent
  • Key of right child is gt key of parent

16
BST searching
  • Start at root
  • Current root
  • If new key is lt current.key
  • Go to left
  • If new key is gt current.key
  • Go left
  • If new key current.key
  • done

17
BST insertion
  • Insertions are always done as leaf nodes
  • Do search
  • If not found and lt current.key add as left
    child of current
  • Otherwise add as right child of current

18
BST Insertions
  • This can be done iteratively or recursively
  • Since a tree has a recursive definition this is
    more naturally done recursively

19
BST Recursive Insert
void InsertNode(Node treeNode, Node newNode)
if (treeNode NULL) treeNode
newNode else if (newNode-gtkey lt treeNode-gtkey)
InsertNode(treeNode-gtleft,
newNode) else
InsertNode(treeNode-gtright, newNode)
Write a Comment
User Comments (0)
About PowerShow.com