32
CS 2133: Data Structures Binary Search Trees

CS 2133: Data Structures

  • Upload
    tyme

  • View
    54

  • Download
    0

Embed Size (px)

DESCRIPTION

CS 2133: Data Structures. Binary Search Trees. Definition of a Tree. A Binary tree is a set T of nodes such that either 1. T is empty or 2. T is partitioned into three disjoint subsets. A single node r, the root - PowerPoint PPT Presentation

Citation preview

Page 1: CS 2133: Data Structures

CS 2133: Data Structures

Binary Search Trees

Page 2: CS 2133: Data Structures

Definition of a Tree

A Binary tree is a set T of nodes such that either

1. T is empty or

2. T is partitioned into three disjoint subsets.

A single node r, the root

Two (possibly empty) sets that are binary trees, called left and

right subtrees of r.

T T

Tl Tr

T

Page 3: CS 2133: Data Structures

Structure of a Tree

struct Node{ int value; Node * left; Node * right;}

root

5

4

3 10 7 9

6class Tree{ Node * root;public: Tree();//constructor Insert(int); Print();}

Page 4: CS 2133: Data Structures

Binary Search Trees

Binary Search Trees (BSTs) are an important data structure for manipulating dynamically changing data sets.

Each node has the following fields: value: an identifying field inducing a total ordering left: pointer to a left child (may be NULL) right: pointer to a right child (may be NULL) p: (sometimes) pointer to a parent node (NULL for

root)

Page 5: CS 2133: Data Structures

Binary Search Trees

BST property: Value of nodes in left subtree <= roots value

Value of nodes in right subtree > roots value Example: F

B H

KDA

Page 6: CS 2133: Data Structures

Inorder Tree Walk

What does the following code do?TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]);

A: prints elements in sorted (increasing) order This is called an inorder tree walk

Preorder tree walk: print root, then left, then right Postorder tree walk: print left, then right, then root

Page 7: CS 2133: Data Structures

Inorder Tree Walk

Example:

How long will a tree walk take? Prove that inorder walk prints in monotonically

increasing order

F

B H

KDA

Page 8: CS 2133: Data Structures

Operations on BSTs: Search

Given a key and a pointer to a node, returns an element with that key or NULL:

Node * TreeSearch(Node * ptr,int k){ if (ptr == NULL)return NULL; if (ptr->value==k) return ptr; if (k < ptr->value) return TreeSearch(ptr->left, k); else return TreeSearch(ptr->right, k);

}

Page 9: CS 2133: Data Structures

BST Search: Example

Search for D and C:

F

B H

KDA

Page 10: CS 2133: Data Structures

Operations of BSTs: Insert

Adds an element x to the tree so that the binary search tree property continues to hold

The basic algorithm Like the search procedure above Insert x in place of NULL Use a “trailing pointer” to keep track of where you

came from (like inserting into singly linked list)

Page 11: CS 2133: Data Structures

BST Insert: Example

Example: Insert C

F

B H

KDA

C

Page 12: CS 2133: Data Structures

Recursive Insert

void BST<DataType>::InsertAux(BinNodePointer & locptr, const DataType & item)

{if(locptr==0) locptr=new BSTNode(item);

else

if (item < locptr->data)

InsertAux(locptr->left, item);

else

InsertAux(locptr->right, item);

}

Page 13: CS 2133: Data Structures

BST Search/Insert: Running Time

What is the running time of TreeSearch() or insertAux()?

A: O(h), where h = height of tree What is the height of a binary search tree? A: worst case: h = O(n) when tree is just a

linear string of left or right children We’ll keep all analysis in terms of h for now Later we’ll see how to maintain h = O(lg n)

Page 14: CS 2133: Data Structures

BST Destroy the tree

BinaryTree::~BinaryTree(){destroyTree(root)}

Void BinaryTree::destroyTree(TreeNode *& treePrt) {

if (tree{tr != NULL;)

{ destroyTree(treePtr->leftChildPtr);

destroyTree(treePtr->rightChildPtr);

delete treePtr;

treePtr=NULL;

}

}

Page 15: CS 2133: Data Structures

BST Operations: Delete

Deletion is a bit tricky 3 cases:

x has no children: Remove x

x has one child: Splice out x

x has two children: Swap x with successor Perform case 1 or 2 to delete it

F

B H

KDA

CExample: delete Kor H or B

Page 16: CS 2133: Data Structures

Delete Algorithm

void TreeDelete(Node *& root, int x)

{ if(root!=NULL){

if(x<root->val)TreeDelete(root->left, x);

else if(x>root->val)TreeDelete(root->right, x);

else { // we have found it so lets delete it

// tree points at it right!?

// If No children we just delete it

if(root->left==NULL && root->right==NULL){

delete(root);

root=NULL;

return;

}

Page 17: CS 2133: Data Structures

Delete Continued with one child

// Check to see if the node to delete has only one child

if(root->left==NULL){ // Then splice in the right side

tree_ptr=root->right;

delete root;

root=tree_ptr;

}else if (root->right==NULL){ // splice left

tree_ptr=root->left;

delete(root);

root=tree_ptr;

} else

// both children exist! so...

Page 18: CS 2133: Data Structures

Delete Continued with two children

// Here root has two children

// We first find the successor

{ tempPtr=root->right; // Go right

// and the all the way to the left

while(tempPtr->left!=NULL){

predPtr=tempPtr;

tempPtr=tempPtr->left;

}

root->val=tempPtr->val; // Copy the value up to the node

if(root->right==tempPtr)root->right=tempPtr->right;

else predPtr->left=tempPtr->right;

delete tempPtr;

}

root

predtemp

Page 19: CS 2133: Data Structures

BST Operations: Delete

Why will case 2 always go to case 0 or case 1? A: because when x has 2 children, its

successor is the minimum in its right subtree Could we swap x with predecessor instead of

successor? A: yes. Would it be a good idea? A:See the Eppinger paper!!

Page 20: CS 2133: Data Structures

Sorting With Binary Search Trees

Informal code for sorting array A of length n:BSTSort(A) for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

What will be the running time in the Worst case? Average case?

Page 21: CS 2133: Data Structures

Left Child Right Sibling Representation

F

B H

KDA

C

H

F

A

B

D H

C

RCLS format Normal format

Really is a binary way to represent an n ary tree.Also each child has a pointer to its parent.

Page 22: CS 2133: Data Structures

Recursive Traversal of a LCRS Tree

H

F

A

B

D H

C

Traverse(t)

{

if (t != NULL)

then Traverse(t.child())

If (t.sibling() !=NULL)

then Traverse(t.sibling())

}

Page 23: CS 2133: Data Structures

Algorithm BT->LCRS Format

Can you design an algorithm that will convert

a binary tree to the left child right sibling

format?

Page 24: CS 2133: Data Structures

Saving a binary search tree in a file

We can do this so the tree gets restored to the original shape. How do we do this?

save in preorder ! Or we can restore it in a balanced shape

save in inorder and ?

Page 25: CS 2133: Data Structures

Loading a balanced tree froma sorted list

readTree(Node *& treePtr,int n)

{

if (n>0)

{ // read in the left subtree

treePtr=new Node(NULL,NULL);

readTree(treePtr->left,n/2);

cin>> treePtr->val;

// read in the right subtree

readTree(treePtr->right,(n-1)/2);

}

Page 26: CS 2133: Data Structures

Level Order Traversal

How do you perform an level by level traversal?

Q.enq(root)

While(Q not empty){

x = Q.deq();

Print x;

Q.enq(left_child);

Q.enq(right_child);

}

F

B H

KDA

NOTE: This uses a queue as its support data structure

Page 27: CS 2133: Data Structures

Huffman Codes

Variable length coding schemes. These schemes represent characters that occur more frequently with shorter codes. We thus attempt to minimize the expected length of the code for the character.

Expected length =

n

iiinn lwlwlwlw

12211 ...

The weights represent the probability of occurrence of the character within the coded string. It is a value between 0 and 1.

Page 28: CS 2133: Data Structures

Constructing the Code

1. Initialize a list of one-node binary trees containing the weights of each char

2. Do the following n-1 times

Find two trees T’ and T’’ in this list with minimal weight

Replace these two trees with a binary tree whose root is w’+w’’, and label the

pointers to these trees with 0 and 1

W’+w’’0 1

T’ T’’

Page 29: CS 2133: Data Structures

Example Construction

.35 .25 .20 .10 .08 .02

.10

.20

.40

.60

1.0

0

0

0

00

1

1

1

11

A B C D E F

A=00B=01C=10D=110E=1110F=1111

Also,this isImmediately Decodable!

Page 30: CS 2133: Data Structures

Expression Trees

+

*

+

W

+

YX 3

5

W + 5 * X + 3 + Y inorder traversal

((W+5)*X + (3+Y)) with parens

How about preorder and postorder traversals?

Page 31: CS 2133: Data Structures

Problem

A preorder traversal of a binary tree producedADFGHKLPQRWZ

and an inorder traversal producedGFHKDLAWRQPZ

Draw the binary tree.

Page 32: CS 2133: Data Structures

Program to Build Treevoid BuildTree(string in,string post){

char root;int len=in.length();

// NOTE: Last char of postorder traveral is the root of the treeif(len==0)return;

root=post[len-1];cout<<root; // print the root// Search for root in the inorder traversal listint i=0;while(in[i]!=root)i++; // skip to the root

// i now points to the root in the inorder traversal// The chars from 0 to i-1 are in the left subtree and // the chars from i+1 to len_in-1 are in the right sub tree.// Process left sub treeBuildTree(in.substr(0,i),post.substr(0,i));//Process right sub treeBuildTree(in.substr(i+1,len-i-1), post.substr(i,len-i-1));

}

0 1 2 . . .

substr(start, len)