Documente Academic
Documente Profesional
Documente Cultură
Binary Trees
Chapter Objectives
y Learn about binary trees y Explore various binary tree traversal algorithms y Learn how to organize data in a binary search tree y Discover how to insert and delete items in a binary search
tree y Explore nonrecursive binary tree traversal algorithms y Learn about AVL (height-balanced) trees
Binary Trees
y Definition: A binary tree, T, is either empty or such that:
y T has a special node called the root node; y T has two sets of nodes, LT and RT, called the left subtree and
Binary Tree
Binary Trees
Following class defines the node of a binary tree: protected class BinaryTreeNode { DataElement info; BinaryTreeNode llink; BinaryTreeNode rlink; }
Nodes
y For each node:
y Data is stored in info y The reference to the left child is stored in llink y The reference to the right child is stored in rlink
10
11
node y Height of a binary tree: number of nodes on the longest path from root to node
12
13
14
Copy Tree
y Useful operation on binary trees is to make identical copy of
binary tree y Method copy useful in implementing copy constructor and method copyTree
15
Method copy
BinaryTreeNode copy(BinaryTreeNode otherTreeRoot) { BinaryTreeNode temp; if(otherTreeRoot == null) temp = null; else { temp = new BinaryTreeNode(); temp.info = otherTreeRoot.info.getCopy(); temp.llink = copy(otherTreeRoot.llink); temp.rlink = copy(otherTreeRoot.rlink); } return temp; }//end copy
16 Data Structures Using Java
17
Traversals
y Inorder
y Traverse the left subtree y Visit the node y Traverse the right subtree
y Preorder
y Visit the node y Traverse the left subtree y Traverse the right subtree
18
Traversals
y Postorder
y Traverse the left subtree y Traverse the right subtree y Visit the node
19
20
21
22
23
right subtree of T, respectively y Key in root node larger than every key in left subtree and smaller than every key in right subtree y LT and RT are binary search trees
25
26
27
28
29
current = root;
2.
while(current is not NULL or stack is nonempty) if(current is not NULL) { push current onto stack; current = current.llink; } else { pop stack into current; visit current; } //visit the node //move to the right child current = current.rlink;
30
31
current = root; v = 0;
if(current is NULL) the binary tree is empty if(current is not NULL) a. push current into stack; b. push 1 onto stack; c. current = current.llink; d. while(stack is not empty) if(current is not NULL and v is 0)
{
4.
32
33
Perfect-Balanced Trees)
y A perfectly balanced binary tree is a binary tree such that:
y The height of the left and right subtrees of the root are equal y The left and right subtrees of the root are perfectly balanced
binary trees
34
35
such that:
y The height of the left and right subtrees of the root differ by at
most 1 y The left and right subtrees of the root are AVL trees
36
AVL Trees
37
Non-AVL Trees
38
bf(x) = xr xl if x is left high, bf(x) = -1 if x is equal high, bf(x) = 0 if x is right high, bf(x) = 1 y x violates the balance criteria if |xr xl| > 1
39
40
41
42
43
44
left subtree; the root of the right subtree of x becomes the new root of the reconstructed subtree
y Right rotation at x: certain nodes from the left subtree of x move
to its right subtree; the root of the left subtree of x becomes the new root of the reconstructed subtree
45
46
47
48
49
50
51