{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L08_bst

# L08_bst - COMP152 Spring 2010 D.Y Yeung Binary Search Trees...

This preview shows pages 1–12. Sign up to view the full content.

Binary Search Trees Nyhoff: 12 COMP152 Spring 2010 D.Y. Yeung

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Outline Binary tree terminology Tree traversals: preorder, inorder and postorder Dictionary and binary search tree Binary search tree operations Search Min and max Successor Insertion Deletion Tree balancing COMP152 2
Binary Tree Terminology Go to the supplementary notes COMP152 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Linked Representation of Binary Trees The degree of a node is the number of children it has. The degree of a tree is the maximum of its element degree. In a binary tree, the tree degree is two Each node has two links one to the left child of the node one to the right child of the node if no child node exists for a node, the link is set to NULL COMP152 4 Left child Right child left right data 79 13 32 95 42 16 32 79 / 42 13 / / 95 / / 16 / / root
Binary Trees as Recursive Data Structures A binary tree is either empty … or Consists of a node called the root Root points to two disjoint binary (sub)trees left and right (sub)trees COMP152 5 Anchor Inductive step r left subtree right subtree

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Tree Traversal is also Recursive If the binary tree is empty then do nothing Else N: Visit the root, process data L: Traverse the left subtree R: Traverse the right subtree COMP152 6 Anchor Inductive/Recursive step
3 Types of Tree Traversal If the pointer to the node is not NULL: Preorder: Node , Left subtree, Right subtree Inorder : Left subtree, Node , Right subtree Postorder: Left subtree, Right subtree, Node COMP152 7 Inductive/Recursive step template<class T> void BinaryTree<T>:: PreOrder ( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t) {// Preorder traversal. if (t) {Visit(t); PreOrder(Visit, t->LeftChild); PreOrder(Visit, t->RightChild); } } template <class T> void BinaryTree<T>:: InOrder ( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t) {// Inorder traversal. if (t) {InOrder(Visit, t->LeftChild); Visit(t); InOrder(Visit, t->RightChild); } } template <class T> void BinaryTree<T>:: PostOrder ( void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t) {// Postorder traversal. if (t) {PostOrder(Visit, t->LeftChild); PostOrder(Visit, t->RightChild); Visit(t); } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Traversal Order Given expression A – B * C + D Child node: operand Parent node: corresponding operator Inorder traversal: infix expression A – B * C + D Preorder traversal: prefix expression + - A * B C D Postorder traversal: postfix or RPN expression A B C * - D + COMP152 8 - * + B D C A
Preorder, Inorder and Postorder Traversals COMP152 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A Fast Way for Tree Traversal You may eye-ball the solution without using recursion. First emanating from each node a “hook.” Trace from left to right an outer envelop of the tree starting from the root. Whenever you touch a hook, you print out the node: Preorder: put the hook to the left of the node Inorder: put the hook vertically down at the node Postorder: put the hook to the right of the node COMP152 10
Another Example (This is a Search Tree) Inorder (Left, Visit, Right): 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 Preorder (Visit, Left, Right): 15, 6, 3, 2, 4, 7, 13, 9, 18,17, 20 Postorder (Left, Right, Visit): 2, 4, 3, 9, 13, 7, 6, 17, 20,18, 15 COMP152 11

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 69

L08_bst - COMP152 Spring 2010 D.Y Yeung Binary Search Trees...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online