Title: Trees, Binary Trees, and Binary Search Trees
1Trees, Binary Trees, and Binary Search Trees
2Trees
- Non-linear structure
- i.e., one item can be connected to multiple other
items in a non-linear way - tree one parent can have multiple children, but
each child can only have one parent - Examples
- Operating system directory structure
- e.g., Linux, / has subdirs. /bin, /usr, /home,
/var, etc.
3Tree Terminology
- Tree recursively defined as empty or a root
node with zero or more sub-trees - Node a holder for data plus edges to children
- Root a pointer to the first node, if it exists,
or NULL - Leaf node a node with no children
- Internal node a node with one or two children
- Edge connects a parent node to a child node
- Path sequence of edges between two nodes
- Height longest path in a tree from root to any
other node - Depth number of edges from root to a node
4Binary Tree
- Tree in which each node has at most 2 children (2
child pointers, each of which may point to a
sub-tree or be null) - Depth of binary tree with n nodes is much less
than n - // --- Sample Declaration - TREENODE.H
- template lttypename Tgt
- class Tree_Node
- T data // T gt template data type parameter
- Tree_NodeltTgt left
- Tree_NodeltTgt right
-
-
- // --- BINTREE.H
- template lttypename Tgt
- class BinTree
- Tree_NodeltTgt root
-
5Applications of Binary Tree
- Expression Tree
- Can traverse in different ways to get prefix,
infix and postfix expressions - Data storage tree
- Store values in nodes
- Problem search in basic binary tree is O(N)
- Value could be anywhere in tree
- No better than a list
6Binary Search Trees
- Assume each data element has some key value
- In a BST, for every node, the key in that node is
greater than all keys found in the left subtree
and less than all keys found in the right subtree
(again, rule applied recursively) - All nodes can be considered ordered (i.e., sorted)
7BSTs - More
- Average depth is log2N
- Declarations - same as for binary tree (TreeNode,
BinarySearchTree) - Function Definitions
- Make_Empty O(N)
- Find O(log2N)
- Find_Min / Find_Max O(log2N)
- Insert O(log2N)
- Remove O(log2N)
8BST Traversal
- 4 types of traversal In-Order, Pre-Order,
Post-Order, Level-by-Level - In-Order Traversal process left subtree, root,
right subtree - template lttypename Tgt
- void BSTltTgtInOrder(TreeNodeltTgt pTree)
-
- if (pTree NULL) // base case
-
- else // if (pTree ! NULL) // recursive case
-
- InOrder(pTree-gtleft)
- Process(pTree-gtdata) // print, whatever
- InOrder(pTree-gtright)
-
-
9(No Transcript)
10(No Transcript)
11(No Transcript)
12(No Transcript)
13BST Traversal (2)
- Pre-Order Traversal (root, left, right)
- move Process(pTree-gtdata) to first of 3
14(No Transcript)
15(No Transcript)
16(No Transcript)
17(No Transcript)
18(No Transcript)
19(No Transcript)
20postfix
21BST Traversal (3)
- Level-by-Level Traversal uses a queue
- add root (pointer) to queue
- do while queue is not empty
- dequeue and process node
- put children (pointers) in queue
22(No Transcript)
23(No Transcript)
24Sample Method Implementation
- Count the nodes in a BST
- idea
- base case if tree is empty, return 0
- recursive case if tree is not empty, return 1
(for current node) plus count from left sub-tree
plus count from right sub-tree - Count the leaf nodes
- Count the non-leaf nodes
25Sample Method Implementation (2)
- // find item under tree at ptr return pointer
to node if found, - // NULL if not found
- template ltclass Tgt
- NodeltTgt BSTltTgtFind (T item, NodeltTgt ptr)
-
- // base case(s)
- ???
- // recursive case(s)
- ???
-
26Sample Method Implementation (3)
- template ltclass Tgt
- void BSTltTgtInsert(T item, NodeltTgt ptr)
-
- // base case(s)
- ???
- // recursive case(s)
- ???
-
27Sample Method Implementation (4)
- template ltclass Tgt
- void BSTltTgtRemove(T item, NodeltTgt ptr)
- // trickier!
- // what are easy and hard cases?
- // base case(s)
- ???
- // recursive case(s)
- ???
-
28Recursive Functions in Classes
- Declaration BST myTree
- Users Point of View
- myTree.count( ) // no parameters
- Implementers Point of View
- Need to pass a pointer, to allow parameter to
change and recursion to work - myTree.count(somePtr) // root or lower-level
ptr - Which one do we need to implement the class?
- Need both!
- void count() count(root) // public
- void count(NodePtr ptr) // protected with
the full // base and recursive work
29Binary Search Tree Applications
- Any situation where you have ordered values and
want logarithmic time search / insertion /
deletion - Examples
- Spell-check
- Index to larger data objects
- Implementation for ordered collection (e.g., for
set in STL)
30Binary Tree Applications - Heapsort
Java Demo
31Sorted Values Allow For
- Interesting (and VERY fast) search algorithms!
- Interpolation Search
- Fibonacci Search
32Interpolation Search O(loglogN)
public static int interpolationSearch(int
table, int x) int low 0 int high
table.length-1 int mid
while(tablelow lt x tablehigh gt x) //
Div truncates mid low
(x-tablelow)/(tablehigh-tablelow)(high-low)
if (tablemid lt x) low
mid 1 else if(tablemid gt x)
high mid - 1 else
return mid if (tablelow x)
return low else return -1 // Not
found
33Interpolation Search
low high Table 0 1 2
. 59 3 23 34
104 x 87 mid 0 (87 3)/(104-3)
(59-0) (54 / 101) 59 // 83 of range
104 48 mid low (x-tablelow)/(table
high-tablelow)(high-low)
34Fibonacci Search
- Idea
- - Not to use the middle element at each step (as
in binary search) - Guess more precisely where the key being sought
falls within the - current interval of interest.
- Instead of splitting the array in the middle,
this implementation splits the array using
fibonacci numbers. - To calculate middle index
- Binary search uses division.
- Fibonacci search uses addition/subtraction of
Fibonacci numbers. -
35Fibonacci Search
- Index
- 0 1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 - x
- Fk-1 Fk
- 0 1 1 2 3 5 8 13 21 34
size of file - Fk-2
- Find mid, i.e., look at Fibonacci numbers (Fn-1)
- Is x in left portion or right portion?
- If left, and mid Fk,
- new mid Fk, - Fk-2 ? 13 -5 8
- If x is to the right, then
- new mid Fk, Fk-1 ? 13 8 21