Binary Tree Illustrated
Binary Tree Illustrated
Binary Trees
Outline
Tree Structures Tree Node Level and Path Length Binary Tree Definition Binary Tree Nodes Binary Search Trees Locating Data in a Tree Removing a Binary Tree Node stree ADT Application of Binary Search Trees - Removing Duplicates Update Operations Insert & remove an item
2
Associative containers
Sequence containers (eg. array, vector, list)
access items by position, A[i] or V[i] Associative containers (eg. Sets and maps) access items by key (or by value).
Objects are added, removed, or updated by value rather than by position. The underlying data structure for STL associative container is the binary search tree, which allow operations (insert, delete, find) in O(logn) time at worst case.
3
Tree Structures
Arrays, vectors, lists are linear structures, one element follows another. Trees are hierarchical structure.
President -CEO
Sales Manager
Shipping Supervisor
4
Tree Structures
+
Tree Terminology
Tree is a set of nodes. The set may be empty. If not empty, then there is a distinguished node r, called root, all other nodes originating from it, and zero or more non-empty subtrees T1,T2, ,Tk, each of whose roots are connected by a directed edge from r. (inductive definition)
r
T1
T2
Tk
6
Tree Terminology
A
root
A leaf node is a node without any children while an interior node has at least one child.
(b)
I J
A B E C F D
Level: 3
8
Binary Tree
In a binary tree, no node has more than two children, and the children are distinguished as left and right. A binary tree T is a finite set of nodes with one the following properties: 1. T is a tree if the set of nodes is empty. 2. The set consists of a root R and exactly two distinct binary trees. The left subtree TL and the right subtree TR, either or both subtree may be empty.
9
Size? depth?
11
Complete or noncomplete?
A
12
Complete or noncomplete?
A
13
Complete or noncomplete?
A
Complete or noncomplete?
A
15
Hint: At level k can have at most 2k nodes Proof (by induction on depth)
16
Find depth
Whats the depth of a complete binary tree
with n nodes?
A perfect complete BT with depth d: a CBT with 2d+1-1 nodes The smallest CBT with depth d: (2(d-1)+1-1)+1=2d
17
Implementing of BT
A binary tree node contains a data value,
called nodeValue, and two pointers, left and right. A value of NULL indicates an empty subtree. Declaration of CLASS tnode, d_tnode.h
left A right
A
left B right left C right
C
left D right left E right
left
right
left
right
left
right
H
Abstract Tree Model
18
Building a BT
A binary tree consists of a collection of
dynamically allocated tnode objects whose pointer values specify links to their children. Example 1: 4 q
8 p
Example 2:
20 p Build a tree from the bottom up: allocate the children first and then the parent
10 root 30 r
19
40 q
Tree traversals
In a traversal, we visit each node in a tree in
some order.
visit means perform some operation at the node. (eg. print the value, change the value, remove the node) Recursive: pre-order, in-order, post -order Iterative: level-order
20
Traverse the left subtree (go left); Visit the node Traverse the right subtree (go right)
B
E
C F
21
access elements by levels, with the root coming first (level 0), then the children of the root (level 1), followed by the next generation (level 2), and so forth. usually done as iterative algorithm using a queue A
B D G H E I C F
Example:
22
Count leaves of BT
A node in a tree is a leaf if it has no children.
of children The order of the scan irrelevant Example: using pre-order pattern (n)
23
Depth of BT
The depth of a tree is the length of the longest path from root to any node. 1+ max{depth(TL), depth(TR)} use post-order traversal (n)
24
Copy BT
Make new nodes for the copy
25
nodes in both subtrees, then delete the node Clear BT: deletes all nodes, then points the root to NULL (delete the root node)
26
in which, at every node, the data value in the left subtree are less than the value at the node and the value in the right subtree are greater. Not allow duplicated value
10 15 30
37 65
53
30 50 59 62
27
Build a BST
The first element entering a BST becomes the root node Subsequence elements entering the tree:
If the value of the new element is less than the value of the current node, proceed to the left subtree of the node
If the left subtree is not empty, repeat the process by comparing item with the root of the subtree If the left subtree is empty, allocate a new node with item as its value, and attach the node to the tree as the left child
If the value of the new element is greater than the value of the current node, proceed to the right subtree of the node
If the right subtree is not empty, repeat the process by comparing item with the root of the subtree If the right subtree is empty, allocate a new node with item as its value, and attach the node to the tree as the right child
If the value of the new element is equal to the value of the current node, perform no action (no duplicate)
28
Examples
Example 1: Start with empty tree, and insert
35,18,25, 48,72, 60 in sequence Example 2: Start with empty tree, and Insert 18,25,48,60,72 Example 3: Start with empty tree, and insert 72,60, 48,35,25,18 Insert a node: O(depth)
50 30 25 10 15 32 35 37
30
55 53 60 62
25
\\
10 15
//
37 30 65
Delete node 25
30
10 15 30
65
37 65
31
CLASS stree
Constructors
d_stree.h
stree(); Create an empty search tree. stree(T *first, T *last); Create a search tree with the elements from the pointer range [first, last).
CLASS stree Opertions d_stree.h
void displayTree(int maxCharacters); Display the search tree. The maximum number of characters needed to output a node value is maxCharacters. bool empty(); Return true if the tree is empty and false otherwise.
32
CLASS stree
Opertions
d_stree.h
int erase(const T& item); Search the tree and remove item, if it is present; otherwise, take no action. Return the number of elements removed. Postcondition: If item is located in the tree, the size of the tree decreases by 1.
void erase(iterator pos); Erase the item pointed to the iterator pos. Precondition: The tree is not empty and pos points to an item in the tree. If the iterator is invalid, the function throws the referenceError exception. Postcondition: The tree size decreases by 1.
33
CLASS stree
Opertions
d_stree.h
void erase(iterator first, iterator last); Remove all items in the iterator range [first, last). Precondition: The tree is not empty. If empty, the function throws the underflowError exception. Postcondition: The size of the tree decreases by the number of items in the range.
iterator find(const T& item); Search the tree by comparing item with the data values in a path of nodes from the root of the tree. If a match occurs, return an iterator pointing to the matching value in the tree. If item is not in the tree, return the iterator value end().
34
CLASS stree
Opertions
d_stree.h
Pair<iterator, bool> insert(const T& item); If item is not in the tree, insert it and return an iteratorbool pair where the iterator is the location of the new element and the Boolean value is true. If item is already in the tree, return the pair where the iterator locates the existing item and the Boolean value is false. Postcondition: The size of the tree is increased by 1 if item is not present in the tree. int size(); Return the number of elements in the tree.
35
v
7 3 2 5 3 2 9 3 2
3 5
v
2 3 5 7 9
Use a BST as a filter: 1) Use vector iterator to copy vector elements to BST 2) Clear the vector 3) Use stree iterator to re-fill the vector
Time complexity?
36
d_video.h, prg10_5
37
findNode( ) findNodeRec()
38
location, and return iterator-bool pair if item is in the tree, return a pair whose iterator component points at the existing item and whose bool component is false. if item is not in the tree, insert it and return a pair whose iterator component points at item and whose bool component is true. And the tree size increases by1 Iteratively traverses the path of the left and right subtress Maintains the current node and parent of the current node Terminates when an empty subtree is found New node replaces the NULL subtree as a child of the parent
39
parent
20
25 35
t
40
12
40
parent
40
12
41
p arent
40
12
32
42
43
Case 1: D is a leaf
Update the parent node P to have an empty subtree
Before 40 After 40
30
65
30
65
25
35
50
25
35
50
10
26
33
26
33
29 28
34 28
29
34
Afte r
40
P
25
30
65
P
25
30
65
D
26 33
35
50
R
26
33
50
10
10
34
29 28
34
29 28
Delete node 35 with only a left child: Node R is the left child.
45
40
40
30
65
30
65
25
35
50
25
35
50
10
26
33
10
29
33
29
34
28
34
28
Delete node 26 with only a right child: Node R is the right child.
46
if (rNodePtr != NULL) // D was not a leaf // the parent of R is now the parent of D rNodePtr->parent = pNodePtr;
}
47
After replacing D by R
pOfRNodePtr = dNodePtr
65
35
50
29
33
25
35
50
25
35
50
10
26
33
10
26
33
29 28
34
29 28
34
Orphaned subtrees.
50
Delete R from DR
Replace D with R
Before replacing D by R P pNodePtr 40 D rNodePtr pNodePtr R R 33 30 65 33 65 After replacing D by R P 40
rNodePtr
26
35
50
26
35
50
10
29
34
10
29
34
28
28
51
Summary Slide 1
- trees
- hierarchical structures that place elements in nodes along branches that originate from a root. - Nodes in a tree are subdivided into levels in which the topmost level holds the root node.
- Any node in a tree may have multiple successors at the next level. Hence a tree is a non-linear structure.
- Tree terminology with which you should be familiar: parent | child | descendents | leaf node | interior node | subtree.
52
52
Summary Slide 2
- Binary Trees
- Most effective as a storage structure if it has high density
- ie: data are located on relatively short paths from the root. - A complete binary tree has the highest possible density
- an n-node complete binary tree has depth int(log2n). - At the other extreme, a degenerate binary tree is equivalent to a linked list and exhibits O(n) access times.
53
53
Summary Slide 3
54
Summary Slide 4
until finding a NULL subtree make it easy to build a binary search tree that does not allow duplicate values.
55
55
Summary Slide 5
56
56