Académique Documents
Professionnel Documents
Culture Documents
A Taxonomy of Trees
General Trees any number of children / node Binary Trees max 2 children / node
Applications:
arithmetic expressions decision processes searching
A
We call the children of an internal node left child and right child Alternative recursive definition: a binary tree is either
a tree consisting of a single node, OR a tree whose root has an ordered pair of children, each of which is a binary tree B
Update methods may be defined by data structures implementing the BinaryTree ADT
Prove by induction.
2i 2 k 1 1
i 0 k
2 4 8 9 10 6 11 12 13
7 14 15
2 4 8 9 10 6 11 12 13
7 14 15
Parent of node i is node i / 2, unless i = 1. Node 1 is the root and has no parent.
4
8 9 10
5 11 12
6
13 14
7 15
Left child of node i is node 2i, unless 2i > n, where n is the number of nodes. If 2i > n, node i has no left child.
2 4 8 9 10 6 11 12 13
7 14 15
Right child of node i is node 2i+1, unless 2i+1 > n, where n is the number of nodes. If 2i+1 > n, node i has no right child.
1 2 4 8 9 3 2 7 8 4 5
1 3 6 7
10
11
12 13 14 15
Adopt convention that we traverse left before right, only 3 traversals remain
lRr, lrR, Rlr inorder, postorder, preorder
Inorder Traversal
In an inorder traversal a node is visited after its left subtree and before its right subtree
Algorithm inOrder(v) if isInternal (v) inOrder (leftChild (v)) visit(v) if isInternal (v) inOrder (rightChild (v))
8
6 2 1 3 4 5 7
2 a 1 3 b
Algorithm inOrder (v) if isInternal (v){ print(() inOrder (leftChild (v))} print(v.element ()) if isInternal (v){ inOrder (rightChild (v)) print ())}
2 5 1 3 2
<
2 1
6 9 4 8
>
17
34 0
41 1
56 2
63 3
72 4
89 5
95 6
34 0
41 1
56 2
72 89 4 5
95 6
34 0
56 2
72 4
95 6
41
89
34
56
72
95
Binary Tree
typedef struct tnode *ptnode; typedef struct node { short int key; ptnode right, left; }; sample binary search tree code
Elements
Case 1: The Tree is Empty Set the root to a new node containing the item Case 2: The Tree is Not Empty Call a recursive helper method to insert the item 10 > 7
10 7 5 9 10 > 9
10
BST Shapes
The order of supplying the data determines where it is placed in the BST , which determines the shape of the BST Create BSTs from the same set of data presented each time in a different order: a) 17 4 14 19 15 7 9 3 16 10 b) 9 10 17 4 3 7 14 16 15 19
removes a specified item from the BST and adjusts the tree uses a binary search to locate the target item: starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree) removal of a node must not leave a gap in the tree,
7 5 4 6 8 9 10
cursor
7
5 9
10
10
10
cursor
5
Removing 5
parent
parent 7 cursor 9 5 7 9
cursor
5
10
10
Best Case
BST tree = new BST(); tree.insert tree.insert tree.insert tree.insert tree.insert tree.insert tree.insert ("E"); ("C"); ("D"); ("A"); ("H"); ("F"); ("K"); >>>> Items in advantageous order: K H F E D C A
Output:
Worst Case
BST tree = new BST(); for (int i = 1; i <= 8; i++) tree.insert (i);
Output:
Random Case
tree = new BST (); for (int i = 1; i <= 8; i++) tree.insert(random());
Output:
O
H F B
Prevent the degeneration of the BST : A BST can be set up to maintain balance during updating operations (insertions and removals) Types of ST which maintain the optimal performance:
splay trees AVL trees 2-4 Trees Red-Black trees B-trees