Trees - PowerPoint PPT Presentation

About This Presentation
Title:

Trees

Description:

We should be able to construct the tree that produced any pair of traversal paths Insertion search Note: If the tree ... Self-balancing trees AVL Trees Red/Black ... – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 37
Provided by: DickSt1
Category:
Tags: black | insertion | tree | trees

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
Things represented as trees
  • Table of Contents
  • Subassembly diagrams
  • Genealogy diagrams
  • Pedigree diagrams
  • Tournament playoffs
  • Graphics representation
  • Organizational charts

7
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?
8
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
9
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
10
Node Structure (Static)
typedef struct Element data
Link left Link
right Node typedef struct
int numFree int
numInTree Link free
Link root Node
NodesNUMNODES tree
11
Node Structure (Dynamic)
typedef node Tree struct Node
element e Tree
left Tree
right
12
Static or Dynamic ?
  • Static
  • you know the maximum number of nodes
  • not likely to change
  • Dynamic
  • size of the tree will change frequently
  • slightly faster than static
  • traversing pointers is faster than calculating
    addresses for array indexing

13
Traversal
Notice that each node is visited 3 times
A
3
1
Were we to print out the node data in the first
visit to each node the printout would be ABC
2
1
Were we to printout the node data on the second
visit to each node the printout would be BAC
3
3
C
B
1
Were we to printout the node data on the third
visit to each node the printout would be BCA
2
2
These are called the preorder, inorder and
postorder traversals respevtively
14
Pre-order Traversal
void preorder( tnode t) if (t !
NULL) printf ( d , t -gt data )
preorder(t -gt left) preorder(t -gt
right)
15
In-order Traversal
void inorder( tnode t) if (t !
NULL) inorder(t -gt left) printf(
d , t -gt data ) inorder(t -gt right

16
Post-order Traversal
void postorder( tnode t) if (t !
NULL) postorder(t -gt left)
postorder(t -gt right) printf( d , t
-gt data )
17
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.
18
Armed with this information
  • We should be able to construct the tree that
    produced any pair of traversal paths

19
Insertion
Tree insert(Tree p , int v) if (p NULL)
p (Tree) malloc(sizeof(struct
Node)) p-gtdata v p-gtright
NULL p-gtleft NULL else if
(v lt p-gtdata) p-gtleft
insert(p-gtleft,v) else if (v gt p-gtdata)
p-gtright insert(p-gtright,v)
return p
20
search
Tree search(Tree p , int v) if ((p NULL)
(v p-gtdata))
printf("Found\n") return p
if (v lt p-gtdata) return
search(p-gtleft,v) else return
search(p-gtright,v)
21
  • Note
  • If the tree is being used to represent a set the
    search function would be better named isMember( )

22
Priority Queue
  • BST could be used as a Priority Queue
  • Needs functions insert( )
    findMin( ) removeMin( ) or
    findMax( ) removeMax( )
  • Node with minimum priority is leftmost node
  • Node with maximum priority is rightmost node

23
Deletion
  • Three cases we need to consider
  • V has no children
  • Remove v
  • V has one child
  • Swap v with child and do 1
  • V has 2 children
  • Swap v with successor
  • leftmost node in right subtree
  • Do case 1 or case 2 to delete

24
Deletion
20
10
25
30
15
5
12
25
Heaps
  • The heap property
  • max heap the largest key is at the root at each
    node the keys of the children must be less than
    the key of the parent
  • min heap the smallest key is at the root the
    keys of the children must be greater than the key
    of the parent
  • used as a priority queue and the basis for heap
    sort

26
Visualize
a one dimensional array as a tree as follows (the
array contains the keys)
0
0 1 2 3 4 5 6
1
2
3
4
5
6
27
To Find the child or parent
  • iL (2iP)1
  • iR (2iP)2
  • iP (iC-1)/2

28
Adding a Key
  • Tree is complete (i.e. it fills up in ascending
    index positions)
  • Must keep track of where end of tree currently is
  • Insert new key at end of tree, then bubble it up
    to it proper location by comparing to the
    parents key and swapping if necessary
  • This gives O(log2n) for each add.

29
Deleting a key
  • Swap the last key with the root and remove the
    last key
  • Recursivly push the root down to its proper level
    by comparing it to its children and swapping with
    smallest child
  • This gives O(log2n) for each deletion

30
Insuring O(log2n) performance
  • The main problem with BST is that its hard to
    insure optimum performance.
  • shape of the tree is very dependent on the order
    the keys were inserted in
  • trees tend to degenerate to varying degrees

31
The solution - Self-balancing trees
  • AVL Trees
  • Red/Black Trees
  • 2-3 trees
  • 2-3-4 trees
  • B-Trees

32
AVL Trees
  • Height balanced
  • at every node the allowable difference in height
    between the right and left subtree is one
  • an additional piece of data is required
  • balance factor
  • value 0 right and left subtrees are same
    height
  • value 1 right subtree is one higher
  • value -1 left subtree is one higher
  • value 2 or -2 tree need to be rebalanced

33
AVL Method
  • Insertion is pretty much like a BST recursive
    insertion but on the way out (unwinding the
    recursion) check and adjust the balance factors,
    if a difference of 2 or -2 is found rebalance the
    tree by doing an RR, LL, RL or LR rotation

34
Out of Balance Conditions
  • insertion into left subtree of left child of x
    called LL (mirror image of RR)
  • insertion into right subtree of right child of x
    called RR (mirror image of LL)
  • insertion into left subtree of right child of x
    called LR (mirror image of RL)
  • insertion into right subtree of left child of x
    called RL (mirror image of LR)

35
LL
k2
k1
k2
k1
C
C
A
B
A
B
B
C
A
Node rotateLeft(Node k2) Node k1
k2-gtleft k2 -gtleft k1-gtright
k1-gtright k2 return k1
36
LR
k3
k2
k1
k3
k1
k2
D
B
C
A
A
D
C
B
Node DoubleRotateLeft( Node k3)
k3-gtleft RotateRight(k3-gtleft) return
RotateLeft(k3)
Write a Comment
User Comments (0)
About PowerShow.com