# Hindi

Topics: Binary search tree, Red-black tree, AVL tree Pages: 10 (1866 words) Published: September 16, 2013
Design & Analysis of Algorithms

Unit 2 ADVANCED DATA STRUCTURE

Binary Search Trees
• Support many dynamic set operations
– SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT

• Running time of basic operations on binary search trees
– On average: Θ(lgn)
• The expected height of the tree is lgn

– In the worst case: Θ(n)
• The tree is a linear chain of n nodes

Binary Search Trees
• Tree representation:
– A linked data structure in which each node is an object

• Node representation:
– – – – –

L Key field key Satellite data Left: pointer to left child Left child Right: pointer to right child p: pointer to parent (p [root [T]] = NIL)

parent data

R

Right child

• Satisfies the binary-search-tree property

Binary Search Tree Example
• Binary search tree property:
– If y is in left subtree of x,
then key [y] ≤ key [x]
2 3 5 5 7 9

– If y is in right subtree of x,
then key [y] ≥ key [x]

Traversing a Binary Search Tree
• Inorder tree walk:
– Prints the keys of a binary tree in sorted order – Root is printed between the values of its left and right subtrees: left, root, right

• Preorder tree walk:
– root printed first: root, left, right

• Postorder tree walk: left, right, root
5 – root printed last 3 2 5 7 9

Inorder: 2 3 5 5 7 9 Preorder: 5 3 2 5 7 9 Postorder: 2 5 3 9 7 5

Traversing a Binary Search Tree
Alg: INORDER-TREE-WALK(x) 1. if x ≠ NIL 2. then INORDER-TREE-WALK ( left [x] ) 3. print key [x] 4. INORDER-TREE-WALK ( right [x] )

• E.g.:
3 2 5

5 7 9

Output: 2 3 5 5 7 9

• Running time:
– Θ(n), where n is the size of the tree rooted at x

Binary Search Trees - Summary
• Operations on binary search trees:
– – – – – – – SEARCH PREDECESSOR SUCCESOR MINIMUM MAXIMUM INSERT DELETE O(h) O(h) O(h) O(h) O(h) O(h) O(h)

• These operations are fast if the height of the tree is small – otherwise their performance is similar to that of a linked list

Red-Black Trees
• “Balanced” binary trees guarantee an O(lgn) running time on the basic dynamic-set operations • Red-black-tree – Binary tree with an additional attribute for its nodes: color which can be red or black – Constrains the way nodes can be colored on any path from the root to a leaf • Ensures that no path is more than twice as long as another ⇒ the tree is balanced

– The nodes inherit all the other attributes from the binary-search trees: key, left, right, p

Red-Black-Trees Properties
1. Every node is either red or black 2. The root is black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black • No two red nodes in a row on a simple path from the root to a leaf

5. For each node, all paths from the node to descendant leaves contain the same number of black nodes

Example: RED-BLACK-TREE
26 17
NIL NIL

41 30 38
NIL NIL NIL NIL

47 50
NIL

NIL

• For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs – NIL[T] has the same fields as an ordinary node – Color[NIL[T]] = BLACK – The other fields may be set to arbitrary values

Black-Height of a Node
26
h=1 bh = 1 NIL h=4 bh = 2

17
NIL

41 30
h=2 bh = 1

h=3 bh = 2

NIL NIL

h=1 bh = 1 38 NIL NIL

47

h=2 bh = 1

50
NIL

h=1 bh = 1 NIL

• Height of a node: the number of edges in a longest path
to a leaf

• Black-height of a node x:

bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x

Properties of Red-Black-Trees
• Claim
– Any node with height h has black-height ≥ h/2

• Proof
– By property 4, at most h/2 red nodes on the path from the node to a leaf – Hence at least h/2 are black 26 17 30 38 41 47 50

Property 4: if a node is red then both its children are black

Operations on Red-Black-Trees
• The non-modifying binary-search-tree operations MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR, and SEARCH run in O(h) time – They take O(lgn) time on red-black trees

• What about...

Please join StudyMode to read the full document