trees. faster than linear data structures more natural fit for some kinds of data examples? why a...

24
Trees

Upload: chaya-rycroft

Post on 31-Mar-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Trees

Page 2: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

• Faster than linear data structures

• More natural fit for some kinds of data• Examples?

Why a tree?

Page 3: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Example Tree

root

ActivitiesTeaching Research

Sami’s Home Page

Papers PresentationsCS211CS101

Page 4: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Terminology

• Root• Parent • Child• Sibling• External node• Internal node• Subtree• Ancestor• Descendant

Page 5: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Example Tree

root

ActivitiesTeaching Research

Sami’s Home Page

Papers PresentationsCS211CS101

Root?Parent – papers, activities Children – cs101, research Sibling - teaching

External nodesInternal nodesSubtree – left subtree of research?Ancestor – papers ancestor of activities?Descendant – papers descendant of home?

Page 6: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Ordered Trees

• Linear relationship between child nodes

• Binary tree – max two children per node – Left child, right child

Davidson Truman

Rollins

Taft ZunigaRalsonBrown

root

Page 7: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Another Ordered Binary Tree

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

Page 8: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Tree Traversal

• Pre-order traversal– Visit node, traverse left subtree, traverse right

subtree

• Post-order traversal– Traverse left subtree, traverse right subtree,

visit node

Page 9: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Example

• Pre-order

• Post-order

Davidson Truman

Rollins

Taft ZunigaRalsonBrown

root

Page 10: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Example

• Pre – Rollins, Davidson, Brown, Ralson, Truman, Taft, Zuniga

• Post – Brown, Ralson, Davidson, Taft, Zuniga, Truman, Rollins

Do Trimmer

Rollins

Tilkidjieva YuciusReardonBendersky

root

Page 11: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Another Example

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

• Pre – Brown, Truman, Taft, Ralson, Davidson, Rollins, Zuniga

• Post – Davidson, Rollins, Ralson, Taft, Zuniga, Truman, Brown

Page 12: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

In-order Traversal

• Traverse left subtree, visit node, traverse right subtree– Brown, Davidson, Ralson, Rollins, Taft,

Truman, Zuniga

Davidson Truman

Rollins

Taft ZunigaRalsonBrown

root

Page 13: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Another Example

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

• In-order – Brown, Davidson, Ralson, Rollins, Taft, Truman, Zuniga

Page 14: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Implementation – TreeNode

• Data members?

• Functions?

Name = Rollins

Page 15: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Implementation – Tree

• Data Members

• Functions– Pre/post/in-order print

Smith

Rollinsroot

Brown

Page 16: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

Implementation – Pre-order

void preOrderPrint(TreeNode* curnode) {o.print();if(curnode->getLeftChild() != NULL)

preOrderPrint(curnode->getLeftChild());if(curnode->getRightChild() != NULL)

preOrderPrint(curnode->getRightChild());

}

Tree* t = …; t->preOrderPrint(t->getRoot());

Page 17: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

BSTs

• Elements in left subtree nodes are before (are less than) element in current node

• Element in current node is before (less than) elements in right subtree

Page 18: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

find Operation

• Algorithm for finding element in BST

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

Page 19: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

find Algorithm

if current node is null

return not found

else if target is in current node

return found

else if target is before current node

return find(left child)

else

return find(right child)

Page 20: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

find Complexity

• Worst case

• Best case

• Average case

Page 21: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

insert Operation

• Algorithm for inserting element in BST

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

Page 22: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

insert Algorithm

if new_elt is before current and current left child is null

insert as left child

else if new_elt is after current and current right child is null

insert as right child

else if new_elt is before current

insert in left subtree

else

insert in right subtree

Page 23: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

remove Operation

• Algorithm for removing element in BST

Ralson

Truman

Brown

Taft Zuniga

RollinsDavidson

root

Page 24: Trees. Faster than linear data structures More natural fit for some kinds of data Examples? Why a tree?

remove Algorithm

elt = find node to remove

if elt left subtree is null

replace elt with right subtree

else if elt right subtree is null

replace with left subtree

else

find successor of elt

(go right once and then left until you hit null)

replace elt with successor

call remove on successor