Top Banner
CS 261 Binary Tree Traversals Binary Search Trees by Tim Budd Sinisa Todorovic
28

BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Apr 03, 2018

Download

Documents

nguyentuyen
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

CS 261

Binary Tree Traversals Binary Search Trees

by Tim Budd

Sinisa Todorovic

Page 2: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Tree Traversals • How to examine every node in a tree

• A list is a simple linear structure: – can be traversed either forward or backward

• What order do we visit nodes in a tree?

• Most common traversal orders: – Pre-order – In-order – Post-order

Page 3: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Tree Traversals • All traversal algorithms have to:

– Process node – Process left subtree – Process right subtree Traversal order is determined by the order these operations are done

Page 4: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Tree Traversals

• Six possible traversal orders: 1. Node, left, right à Pre-order 2. Left, node, right à In-order 3. Left, right, node à Post-order

4. Node, right, left 5. Right, node, left 6. Right, left, node

⎫  Subtrees are not ⎬  usually analyzed ⎭ from right to left.

⎫  ⎬  Most common ⎭

Page 5: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

• Process order à Node, Left subtree, Right subtree void preorder(struct Node *node) {

if (node != 0){

process (node->val);

preorder(node->left);

preorder(node->rght);

}

}

Example result: p s a m a e l r t e e

e

l

s

a

a m r

t e

e

Pre-Order Traversal

p

Page 6: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Post-Order Traversal • Process order à Left subtree, Right subtree, Node

void postorder(struct Node *node) {

if (node != 0){

postorder(node->left);

postorder(node->rght);

process(node->val);

}

}

Example result: a a m s l t e e r e p

e

l

s

a

a m r

t e

e

p

Page 7: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

In-Order Traversal • Process order à Left subtree, Node, Right subtree void inorder(struct Node *node) {

if (node != 0){

inorder(node->left);

process(node->val);

inorder(node->rght);

}

}

Example result: a sample tree

e

l

s

a

m r

t

p

a

e

e

Page 8: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Traversals • Computational complexity:

– Each traversal requires constant work at each node (not including recursive calls)

– Order not important

– Iterating over all n elements in a tree requires O(n) time

Page 9: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Traversals • Problems with tree traversals:

– If internal: ties (task dependent) process method to the tree implementation

– If external: exposes internal structure (access to nodes) à Not good information hiding

– Recursive function can’t return single element at a time

– Solution à Iterator (more on this later)

Page 10: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Search Trees

Page 11: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Search Tree

• = Binary trees where every node value is: – Greater than all its left descendants – Less than or equal to all its right descendants – In-order traversal returns elements in sorted

order

• If tree is reasonably full (well balanced), searching for an element is O(log n)

Page 12: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Search Tree: Example

Alex

Abner Angela

Adela Alice Audrey

Adam Agnes Allen Arthur

Abigail

Page 13: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Bag Implementation: Contains

• Start at root

• At each node, compare value to node value: – Return true if match

– If value is less than node value, go to left child (and repeat)

– If value is greater than node value, go to right child (and repeat)

– If node is null, return false

Page 14: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Bag Implementation: Contains

• Traverses a path from the root to the leaf • Therefore, if tree is reasonably full, execution time is O( ?? )

Page 15: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

BST: Contains/Find Example

Alex

Abner Angela

Adela Abigail Alice Audrey

Adam Agnes Allen Arthur

Object to find Agnes

Page 16: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Search Tree (BST): Implementation

struct BSTree { struct Node *root; int cnt; }; void initBSTree(struct BSTree *tree); void addBSTree(struct BSTree *tree, TYPE val); int containsBSTree(struct BSTree *tree, TYPE val); void removeBSTree(struct BSTree *tree, TYPE val); int sizeBSTree(struct BSTree *tree);

Page 17: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Binary Node: Reminder

struct Node { TYPE val; struct Node *left; /* Left child */ struct Node *right; /* Right child */ };

Page 18: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

BST: Add Example

Alex

Abner Angela

Adela Alice Audrey

Adam Agnes Allen Arthur

Before first call to add

Object to add Aaron

“Aaron” should be added here

Aaron

Abigail

Page 19: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Useful Auxiliary Function -- Recursion

Node _addNode(Node current, TYPE val){

if current is null

return new Node with val

else if val < Node.val

leftchild = addNode(leftchild, val)

else

rightchild = addNode(rightchild, val)

return current node

}

Page 20: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Add: Calls Utility Routine

void add(struct BSTree *tree, TYPE val) {

tree->root = _addNode(tree->root, val);

tree->cnt++;

}

What is the complexity? O( ?? )

Page 21: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

BST: Remove

Alex

Abner Angela

Adela Alice Audrey

Adam Agnes Allen Arthur

Abigail

How would you remove Abigail? Audrey? Angela?

Page 22: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Who fills the hole in the tree?

• Answer: the leftmost child of the right child

(smallest element in right subtree)

• Try this on a few values

Page 23: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

BST: Remove Example

Alex

Abner Angela

Adela Abigail Alice Audrey

Adam Agnes Allen Arthur

Before call to remove

Element to remove

Replace with: leftmost(rght)

Page 24: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

BST: Remove Example

Alex

Abner Arthur

Adela Abigail Alice

Adam Agnes Allen

Audrey

After call to remove

Page 25: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Special Case

• What if you don’t have the right child?

• Try removing “Audrey” – Simply, return the left child

Angela

Alice Audrey

Allen Arthur

Page 26: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Useful Routines TYPE _leftmost(struct Node *cur) {

... /*Returns value of leftmost child of current node*/ }

struct Node *_removeLeftmost(struct Node *cur) {

... /*Returns tree with leftmost child removed*/ }

Page 27: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Remove Node removeNode(Node current, TYPE val){

if val == current.val

if rightchild is NULL

return leftchild;

else

replace val with

the leftmost child of the rightchild;

rightchild = removeLeftmost(right);

else if val < current.value

leftchild = removeNode(leftchild, val);

else

rightchild = removeNode(rightchild, val);

return current

}

Page 28: BST1 new - Oregon State Universityclasses.engr.oregonstate.edu/eecs/spring2012/cs261/lectures/BST.pdf · Tree Traversals! • How to examine every node in a tree! • A list is a

Complexity • Running down a path from root to leaf

• What is the time complexity? O( ?? )