Title: Trees
1Trees
- Trees
- Binary tree and Binary tree traversals
- Expression trees
- Infix, Postfix, Prefix notation
- Binary Search Trees
- Operations supported
- Complexity
2(Directed) Tree
3Definition of Trees
- /\ is a tree. It is called an empty tree.
- If is a node and
- are non-empty trees where k ? 1, then
- is a tree.
4Notation about Trees
- root, subtrees
- parent, child, sibling
- leaves, internal nodes
- edge
- path, length
- ancestor, descendant
- depth, height
- Root at depth 0,
- One node tree height 0
5Tree criteria
- Always a single root
- A single path to every node
- Doesnt matter how we draw it
6Trees ?
4
7Game tree
Start state for the 8-puzzle X marks the empty
square.
Can move the 3 tile into the empty square, or,
can move the 6 tile into it.
Goal of the game is to place all tiles in some
special order.
8Binary Trees
- Binary tree every node has at most 2 children
(left child and right child) - N-ary tree no node has more than N children.
- Advantages?
- Disadvantages?
- Equivalencies?
9Some Properties of Binary Trees
- Suppose a binary tree has n nodes, and height h.
- The total numbers of nodes h1 ?n ?2h1-1
- The height of the tree (from inequality above)
- log(n1)-1 ? h ? (n-1)
10Implementation of Binary Trees
11Binary Trees
node
LL left link RL Right Link
T
(((ab)(c(d-3)))
12Implementation of N-ary Trees
13Implementation of Trees
Right link to siblings left link to successors
14Some applications
- Expressions ( (731218) (4/3) )
- HTML or XML
- Word documents
3
15Traversing a tree
- Traversing a tree is to visit every node in the
tree
16Binary Tree Traversal
- Preorder
- Root
- Left
- Right
- In order
- Left
- Root
- Right
- Postorder
- Left
- Right
- Root
Preorder (T) if TNIL then
return() Print(T-INFO) Preorder
(T-LL) Preorder (T-RL) Inorder (T) if
TNIL then return() Inorder (T-LL) Print
(T-INFO) Inorder (T-RL) Postorder (T)
if TNIL then return() Postorder
(T-LL) Postorder (T-RL) Print (T-INFO)
17Preorder Traversal
Preorder (T) if TNIL then
return() Print(T-INFO) Preorder
(T-LL) Preorder (T-RL)
18Used for copying a tree
- As in copy constructor for tree class
- Copy the root
- Recursively copy left subtree
- Recursively copy right subtree
- Always have parent node to link new nodes to
19Inorder Traversal
Inorder (T) if TNIL then return() Inorder
(T-LL) Print (T-INFO) Inorder (T-RL)
20Postorder Traversal
( )
postorder
Postorder (T) if TNIL then
return() Postorder (T-LL) Postorder
(T-RL) Print (T-INFO)
21Used to destruct tree
- Return leaf nodes to heap via delete in a bottom
up fashion - When children are returned to heap, then return
the parent to heap - More difficult to do in preorder
22Example of Traversals
Preorder a b c - d 3 prefix
notation Postorder a b c d 3 - Postfix
notation Inorder a b c d - 3 Infix
notation Prefix and postfix notation
unambiguous Infix notation ambiguous.
parenthesis necessary
23Scene graph from graphics
- Tree (graph) nodes are visited in preorder (top
down) - Node might set light or place the camera
- Node might set properties, such as color and
shine - Node might define rotation or translation
- Node might define object geometry to render
24Expression Trees
63
- Postfix
- 1 2 3 4 5
- Prefix
- 1 2 3 4 5
- Infix
- 1 2 3 4 5 30
- ( 1 ( 2 3 ) ) ( 4 5 ) 63
7
9
6
1
4
5
2
3
25Postfix to Expression Tree
- Similar to evaluating postfix expressions
- Evaluating a postfix expression
- Scan the input one symbol at a time
- If it is a number
- push it onto the stack
- If it is an operator,
- pop 2 numbers from the stack
- apply the operator to the 2 numbers
- push the result back onto the stack
26Constructing an expression tree
- 1 2 3 4 5 -
- 1 2 3 4 5 1
- 1 2 3 4 5 1 2
- 1 2 3 4 5 1 2 3
- 1 2 3 4 5 1 6
27Binary Tree Reconstruction
- For a given preorder sequence of binary tree, is
the inorder sequence unique? - Given a preorder sequence of binary tree, we can
construct a lot of BTs according to the
corresponding inorder sequence.
2
1
3
2
1
3
28Example
- 1, 2, 3
- Preorder sequence 123
- Possible inorder sequences are
- 123, 132, 213, 231, 321
29Binary search trees
- Store keys
- all keys in left subtree keys in right subtree
30Search Tree
LEFT sorted
list
31Example
How do we find 8 in this tree?
32Search Operations
- Find( tree, key)
- the pointer to the key (or null pointer)
- FindMin( tree), FindMax( tree)
- What is the running time?
33Find, FindMin, FindMax
34How to insert?
Insert(X, var T) if(T nil) T
newnode(X) else if X
Insert(X, T.LL) if X T.item
Insert(X, T.RL)
35Delete
- 3 cases
- What are they?
- Which are easy?
36Delete 2 children
2
Deletemin from Right child
37Deletion
delete(var T,x) if T NIL then return x
does not exists in T else if (x INFO )
then delete (T-LL,x) else if (x T-INFO )
then delete (T-RL,x) else // T-INFO
x if (T - LL NIL) then T T- RL else
if (T - RL NIL) then T T- LL else
// T has left child, // max of left
child becomes the root
y deletemin (T-RL) T- INFO y
Q How to write delete with
deletemin?
38Deletemin
int deletemin(var T) if T NIL then
return tree is already empty else if
(T - LL NIL) // T has only right child so T
is the min x T - INFO
T T - RL else //
T has a left child, follow left child x
deletemin (T-LL) return
(x) Question How to do deletemax?
39Complexity
- search, insertion, deletemax, deletion
- average case O(log n) ---
- But, only if random insertion!!!!
- worst case O(n)
Question How to improve the performance?
40Balancing Search Tree
- AVL Tree
- for every node in the tree, the height of the
left and right subtree can differ by at most 1. - How to keep it balanced?
- Rotation (single, or double)
- average depth and worst case depth are O(logn)
- Splay Tree
- life time total complexity for m operations is
O(mlogn), where n is the size of the tree - simpler to implement than AVL tree
- B Tree
- each node has between 2 and M children
- Design for small max path
- good for external storage (disks etc.) data
structures
41Testing balanced
- boolean is_balanced (T, var height)
- if TNIL then height -1 return true
- else
- left_b is_balanced (T-LL,
left_height) - right_b is_balanced (T-RL,
right_height) - height max (left_height,
right_height) 1 - if ( left_b and right_b and
(abs(left_height -
right_height) - then return(true)
- else return (false)
-
-
What is the criteria?
2
42Other things
int height (T) if TNIL then return
(-1) else return ( max (height(T-LL),
height(T-RL)) 1)
- int count(T)
- if TNIL then return (0)
- else return (count(T-LL) count(T-RL) 1)
43Good Trees and Bad Trees
h ?(log n)
h ?(n)