Data Structures - CSCI 102
Binary TreeIn binary trees, each Node can point to two other Nodesand looks something like this:
template <typename T>class BTNode {
public://The data section
BTNode
data
T data;left right
//The linksBTNode<T>* left;BTNode<T>* right;
};
6
Copyright © William C. Cheng
7
Copyright © William C. Cheng
G
D
H
E
I
B
Data Structures - CSCI 102
Binary Tree
A
C
F
Trees grown downward in computer science
8
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Tree Terminology
Each BTNode has 0, 1 or 2 Child Nodes that are below it inthe tree
A BTNode that has a child is called that child’s Parent Node
Parent nodes point to child nodes via a branch (child nodesdon’t need to point to parents, but they can if they want)
9
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary TreesA is the Parent of B and C
B is the Left Child of A
C is the Right Child of A
A
Branch
B
Branch
C
The Root Node is the only BTNode in the tree that has noparent node
10
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Tree Terminology
A BTNode is a Leaf Node if it has no children
The Level of a BTNode in the tree is the number ofbranches from the root to the node
The Height of the tree is the number of nodes on thelongest path from root to leaf
11
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Trees
B
A
C
D E
A is the Root Node
C, D, E are Leaf Nodes
Level(A) = 0
Level(B) = 1Level(D) = 2
The Height of the tree is 3
What kind of operations do we need to perform on a binarytree?
12
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Trees
height()size()search()insert()delete()print()
NOTE: Pretty much every operation requires a way totraverse the tree
14
Copyright © William C. Cheng
E
H I
D
G
B C
F
Data Structures - CSCI 102
Binary Tree HeightIf you had to calculate the height of a binary tree, howwould you do it?
Root
A
15
Copyright © William C. Cheng
E
H I
D
G
B C
F
Data Structures - CSCI 102
Binary Tree HeightHeight = 1 + max(Height(Root->left),Height(Root->right))
Root
A
Height ofRoot->left
Root->right
1
Height of
1
18
Copyright © William C. Cheng
E
H I
D
G
B C
F
Data Structures - CSCI 102
Binary Tree TraversalIf you had to print out all the nodes in a binary tree, howwould you do it?
Root
A
If you had to print out all the nodes in a binary tree, howwould you do it?
19
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Tree Traversal
At each node in the tree, you have three choices:
These choices leads to different kinds of traversals
Print the current nodeGo to the left childGo to the right child
In Order: print left, print current, print rightPre Order: print current, print left, print rightPost Order: print left, print right, print current
20
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Tree TraversalIn Order Traversal:
Traverse A’s left subtreePrint ATraverse A’s right subtree
Order of output:GDBHEIACF
E
I
D
G
B
H
A
C
F
Root
Pre Order Traversal:Print ATraverse A’s left subtreeTraverse A’s right subtree
Order of output:ABDGEHICF
E
I
D
G
B
H
A
C
F
Root
22
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Tree Traversal
Post Order Traversal:Traverse A’s left subtreeTraverse A’s right subtreePrint A
Order of output:GDHIEBFCA
E
I
D
G
B
H
A
C
F
Root
24
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Tree Traversal
26
Copyright © William C. Cheng
E
H I
D
G
B C
F
Data Structures - CSCI 102
Binary Tree SearchIf you had to find a particular value in a binary tree, howwould you do it?
Root
A
Search still requires us to potentially look at everysingle node in the treeStill O(n)
So far binary trees are interesting, but they don’t provideany extra benefit
27
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Tree Issues
If you’re going to insert a new Node, where should youput it in the tree?
What’s missing here is some extra organization that makesit easier to find things
template <typename T, typename U>class BTNode {
public:T key; //The key dataU data; //The data
left right//The linksBTNode<T,U>* left;BTNode<T,U>* right;
};
8
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary TreeBTNode can also have a separate key and data
key is used to order the nodes in the treedata actually holds the stuff we care about
BTNode
key
To speed up insertion, removal and search, modify the ideaof a Binary Tree to create a Binary Search Tree (BST)
9
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Trees
Binary Search Trees have one major organizationalproperty (the binary search tree property):
Let X be a Node in a binary search tree
Every Node X is comparable to every other Node via afield we call the key (for now, "data" is the key)If Y is a Node in the left subtree of X:key[Y] <= key[X]
key[Y] > key[X]
If keys must be unique, then we have key[Y] < key[X]You can implement your BST to enforce thisWe will assume that this is the case here
If Y is a Node in the right subtree of X:
11
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree Search
58
7746
30
If you had to find a particular value in abinary search tree, how would you do it?
Ex: find 58
50
60
70
80
Root
If you had to find a particular value in abinary search tree, how would you do it?
12
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree Search
5830
50
46
60
70
77
80
Root
Ex: find 58Start at root
58 == 60? No58 < 60? Yes, go left
Current
13
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree Search
58
7746
30
50
60
70
80
RootIf you had to find a particular value in abinary search tree, how would you do it?
Ex: find 58Start at root
58 == 60? No58 < 60? Yes, go left
Current
58 == 50? No58 < 50? No58 > 50? Yes, go right
14
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree Search
58
7746
30
50
60
70
80
RootIf you had to find a particular value in abinary search tree, how would you do it?
Ex: find 58Start at root
58 == 60? No58 < 60? Yes, go left58 == 50? No58 < 50? No
Current
58 > 50? Yes, go right58 == 58? Yes
Start at the rootWhat will our search algorithm be?
If the key we want is less than the key of the currentnode, search the left subtree
15
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Search
Is the current node what we want?
Otherwise, search the right subtree
If yes, we’re doneIf no...
Should our solution be recursive or iterative?
18
Copyright © William C. Cheng
58
7746
30
50 70
80
Data Structures - CSCI 102
Binary Search Tree Min/MaxHow would you find the minimum/maximum values in abinary search tree?
Root
60
Start at the rootWhat will our findMin algorithm be?
19
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Min/Max
Does the current Node have a left child?If yes, go to the left childIf no, we’re at the minimum
Start at the rootWhat will our findMax algorithm be?
Does the current Node have a right child?If yes, go to the right childIf no, we’re at the maximum
Order of output:30 46 50 58 60 70 77 80
discussed?In Order Traversal
Visits all the nodesin the tree in order
21
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree TraversalNow that we have an implicit ordering to our tree, whathappens with the traversal algorithms we previously
5830
50
46
60
70
77
80
Root
22
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Traversal
Traverse the binary search tree with an in-ordertraversal
Tree SortGiven an unordered list of elements, add them all to abinary search tree
All the nodes come back out sorted!
Binary Search Tree Insertion
24
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Insertion
New nodes are always added in the place of an existingNULL...they never reposition other nodes
1) Create the new node with its data and set both of itspointers to NULL2) If the tree is empty, make the new node the root3) Walk down the tree node by node like you were doinga search
If new value is less than current node, move to leftsubtree, otherwise move to right subtreePotentially reject duplicates
25
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
RootEx: insert 59
Ex: insert 59Make new node
26
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
Root
59New Node
27
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
Root
59New Node
Current
Ex: insert 59Make new nodeStart at root59 < 60; go left
59 > 50; go right
28
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
Root
59New Node
Current
Ex: insert 59Make new nodeStart at root59 < 60; go left
29
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
7746
30
50
60
70
80
Root
59New Node
Current
58
Ex: insert 59Make new nodeStart at root59 < 60; go left
59 > 50; go right59 > 58; go right
30
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
Root
59
Ex: insert 59Make new nodeStart at root59 < 60; go left
59 > 50; go right59 > 58; go rightRight is NULL
Insert new Node
31
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree InsertionHow do you insert a new value into a binary search tree?
58
7746
30
50
60
70
80
Root
59
Ex: insert 59Make new nodeStart at root59 < 60; go left
59 > 50; go right59 > 58; go rightRight is NULL
Insert new Node
Binary Search Tree Removal
32
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Remove
Search the tree for the node that you want to removeWhen node is found, removal breaks down into 3 distinctcases
1) Remove a node with no children2) Remove a node with one child3) Remove a node with two children
Find the node and its parentRemove a node with no children (case #1)
33
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Remove
Set the parent pointer to the node to NULLDelete the node
Case #1 Ex: remove 46
34
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
7746
30
50
60
70
80
Root
35
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
7746
30
50
60
70
80
Root
To remove
Case #1 Ex: remove 46Search for the value
36
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
7746
30
50
60
70
80
Root
To remove
Case #1 Ex: remove 46Search for the valueSet parent’s pointer to NULL
37
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
7746
30
50
60
70
80
Root
To remove
Case #1 Ex: remove 46Search for the valueSet parent’s pointer to NULLDelete the child
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
77
38
Copyright © William C. Cheng
30
50
60
70
80
RootCase #1 Ex: remove 46Search for the valueSet parent’s pointer to NULLDelete the child
Find the node and its parentRemove a node with one child (case #2)
Set the parent pointer to the node’s only childDelete the node
39
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Remove
58
7746
30
50
60
70
80
Root
40
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Case #2 Ex: remove 30
58
7746
30
50
60
70
80
Root
41
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
To remove
Case #2 Ex: remove 30Search for the value
58
7746
30
50
60
70
80
Root
42
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
To remove
Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child
58
7746
30
50
60
70
80
Root
43
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
To remove
Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child
Delete the node
58
50
46
60
70
77
80
Root
44
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Case #2 Ex: remove 30Search for the valuePointer value’s parent tovalue’s only child
Delete the node
Find the nodeRemove a node with two children (case #3)
45
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Remove
Remove node’s successor or predecessorReplace node’s data with successor/predecessor data
The node in the tree with the biggest value less than xPredecessor(x)
The node in the tree with the smallest value greaterthan x
Successor(x)
58
7746
30
50
60
70
80
RootCase #3 Ex: remove 50
46
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
58
7746
30
50
60
70
80
RootCase #3 Ex: remove 50
47
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Search for the valueTo remove
5830
50
46
60
70
77
80
RootCase #3 Ex: remove 50
48
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Search for the valueFind predecessor
Rightmost value in
To remove
left subtree of node
Predecessor
58
77
30
60
70
80
RootCase #3 Ex: remove 50
49
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Predecessor
46
To remove
Search for the valueFind predecessor
Rightmost value inleft subtree of node
Swap values of nodeand its predecessor
Swap
50
58
7750
30
46
60
70
80
Root
50
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Case #3 Ex: remove 50Search for the valueFind predecessor
Rightmost value in
To remove
left subtree of nodeSwap values of nodeand its predecessor
Delete the node viacase #1 or case #2
5830
46
60
70
80
Root
Data Structures - CSCI 102
Binary Search Tree RemoveHow do you remove a value from a binary search tree?
Case #3 Ex: remove 50Search for the valueFind predecessor
Rightmost value in
left subtree of nodeSwap values of nodeand its predecessor
Delete the node viacase #1 or case #2
77
51
Copyright © William C. Cheng
60, 46, 77, 45, 50, 63, 91Insert the following values in an empty tree (in this order)
52
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Construction
What does the resulting tree look like?
53
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (in this order)
60, 46, 77, 45, 50, 63, 91
50 6345
What does the resulting tree look like?Tree is perfectly balanced!
Height = log(n+1)
46
60
91
Root
77
91, 77, 63, 60, 50, 46, 45
Insert the following values in an empty tree (same values aslast time, but different order)
54
Data Structures - CSCI 102
Copyright © William C. Cheng
Binary Search Tree Construction
What does the resulting tree look like?
91Root
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (same values as
last time, but different order)91, 77, 63, 60, 50, 46, 45
What does the resulting tree look like?Insert 91
55
Copyright © William C. Cheng
91Root
77
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (same values as
last time, but different order)91, 77, 63, 60, 50, 46, 45
What does the resulting tree look like?
Insert 91Insert 77
56
Copyright © William C. Cheng
91Root
77
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (same values as
last time, but different order)91, 77, 63, 60, 50, 46, 45
What does the resulting tree look like?Insert 91
63Insert 77
Insert 63
57
Copyright © William C. Cheng
58
Copyright © William C. Cheng
91Root
77
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (same values as
last time, but different order)91, 77, 63, 60, 50, 46, 45
What does the resulting tree look like?Insert 91
63Insert 77Insert 63 60Insert 60
91Root
77
60
50
46
45
59
Copyright © William C. Cheng
Data Structures - CSCI 102
Binary Search Tree ConstructionInsert the following values in an empty tree (same values as
last time, but different order)91, 77, 63, 60, 50, 46, 45
What does the resulting tree look like?Tree is completely unbalanced
63Height = n
Top Related