L08_bst - Binary Search Trees Nyhoff: 12 COMP152 Spring...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Binary Search Trees Nyhoff: 12 COMP152 Spring 2010 D.Y. Yeung 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 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 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); } } 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 A Fast Way for Tree Traversal You may eye-ball the solution without using recursion....
View Full Document

This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 69

L08_bst - Binary Search Trees Nyhoff: 12 COMP152 Spring...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online