Trees
• Faster than linear data structures
• More natural fit for some kinds of data
• Examples?
Why a tree?
Example Tree
root
Activities
Teaching Research
Sami’s Home Page
Papers Presentations
CS211
CS101
Conference
Journal
Terminology
• Root
• Parent
• Child
• Sibling
• External node
• Internal node
• Subtree
• Ancestor
• Descendant
Example Tree
root
Activities
Teaching Research
Sami’s Home Page
Papers Presentations
CS211
CS101
Root?
Parent – papers, activities
Children – cs101, research
Sibling - teaching
External nodes
Internal nodes
Subtree – left subtree of research?
Ancestor – papers ancestor of activities?
Descendant – papers descendant of home?
Ordered Trees
• Linear relationship between child nodes
• Binary tree – max two children per node
– Left child, right child
Davidson
Truman
Rollins
Taft Zuniga
Ralson
Brown
root
Another Ordered Binary Tree
Ralson
Truman
Brown
Taft Zuniga
Rollins
Davidson
root
Tree Traversal
• Pre-order traversal
– Visit node, traverse left subtree, traverse right subtree
• Post-order traversal
– Traverse left subtree, traverse right subtree, visit node
Example
• Pre-order
• Post-order
Davidson
Truman
Rollins
Taft Zuniga
Ralson
Brown
root
Example
• Pre (N,L,R) – Ajay, Bhanu, Dravid, Harini, Eswar,
Chandu, Farhan, Ishaq, Gopi
• Post(L,R,N)– Harini, Dravid, Eswar, Bhanu,
Ishaq, Farhan, Gopi, Chandu, Ajay
In-Order (L, N, R )–
Bhanu
Chandu
Ajay
Farhan
Gopi
Eswar
Dravid
root
Harini
Ishaq
Another Example
Ralson
Truman
Brown
Taft Zuniga
Rollins
Davidson
root
• Pre – Brown, Truman, Taft, Ralson, Davidson, Rollins, Zuniga
• Post – Davidson, Rollins, Ralson, Taft, Zuniga, Truman, Brown
In-order Traversal
• Traverse
• left subtree, visit node, right subtree
– Brown, Davidson, Ralson, Rollins, Taft,
Truman, Zuniga
Davidson
Truman
Rollins
Taft Zuniga
Ralson
Brown
root
A
B C
D E F G
H I J
Pre Order : H, I, D, J, E, B, F, G, C, A
Post Order:
Another Example
Ralson
Truman
Brown
Taft Zuniga
Rollins
Davidson
root
• In-order – Brown, Davidson, Ralson, Rollins, Taft,
Truman, Zuniga

trees.ppt