Sunteți pe pagina 1din 51

Chapter 10

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

Data Structures Using Java

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

right subtree of T, respectively; y LT and RT are binary trees

Data Structures Using Java

Binary Tree

Data Structures Using Java

Binary Tree with One Node

The root node of the binary tree = A LA = empty RA = empty

Data Structures Using Java

Binary Tree with Two Nodes

Data Structures Using Java

Binary Tree with Two Nodes

Data Structures Using Java

Various Binary Trees with Three Nodes

Data Structures Using Java

Binary Trees
Following class defines the node of a binary tree: protected class BinaryTreeNode { DataElement info; BinaryTreeNode llink; BinaryTreeNode rlink; }

Data Structures Using Java

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

Data Structures Using Java

General Binary Tree

11

Data Structures Using Java

Binary Tree Definitions


y Leaf: node that has no left and right children y Parent: node with at least one child node y Level of a node: number of branches on the path from root to

node y Height of a binary tree: number of nodes on the longest path from root to node

12

Data Structures Using Java

Height of a Binary Tree


Recursive algorithm to find height of binary tree: (height(p) denotes height of binary tree with root p):
if(p is NULL) height(p) = 0 else height(p) = 1 + max(height(p.llink),height(p.rlink))

13

Data Structures Using Java

Height of a Binary Tree


Method to implement above algorithm:
private int height(BinaryTreeNode p) { if(p == NULL) return 0; else return 1 + max(height(p.llink), height(p.rlink)); }

14

Data Structures Using Java

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

Data Structures Using Java

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

Binary Tree Traversal


y Must start with the root, then
y Visit the node first

or y Visit the subtrees first


y Three different traversals
y Inorder y Preorder y Postorder

17

Data Structures Using Java

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

Data Structures Using Java

Traversals
y Postorder
y Traverse the left subtree y Traverse the right subtree y Visit the node

19

Data Structures Using Java

Binary Tree: Inorder Traversal

20

Data Structures Using Java

Binary Tree: Inorder Traversal


private void inorder(BinaryTreeNode p) { if(p != NULL) { inorder(p.llink); System.out.println(p.info + ); inorder(p.rlink); } }

21

Data Structures Using Java

Binary Tree: Preorder Traversal


private void preorder(BinaryTreeNode p) { if(p != NULL) { System.out.println(p.info + ); preorder(p.llink); preorder(p.rlink); } }

22

Data Structures Using Java

Binary Tree: Postorder Traversal


private void postorder(BinaryTreeNode p) { if(p != NULL) { postorder(p.llink); postorder(p.rlink); System.out.println(p.info + ); } }

23

Data Structures Using Java

Implementing Binary Trees: class BinaryTree methods


y isEmpty y inorderTraversal y preorderTraversal y postorderTraversal y treeHeight y treeNodeCount y treeLeavesCount y destroyTree y copyTree
24 Data Structures Using Java

Copy Inorder Preorder postorder Height Max nodeCount leavesCount

Binary Search Trees


y Data in each node
y Larger than the data in its left child y Smaller than the data in its right child

y A binary search tree, t, is either empty or:


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

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

Data Structures Using Java

Binary Search Trees

26

Data Structures Using Java

Operations Performed on Binary Search Trees


y Determine whether the binary search tree is empty y Search the binary search tree for a particular item y Insert an item in the binary search tree y Delete an item from the binary search tree

27

Data Structures Using Java

Operations Performed on Binary Search Trees


y Find the height of the binary search tree y Find the number of nodes in the binary search tree y Find the number of leaves in the binary search tree y Traverse the binary search tree y Copy the binary search tree

28

Data Structures Using Java

Nonrecursive Inorder Traversal

29

Data Structures Using Java

Nonrecursive Inorder Traversal: General Algorithm


1.

current = root;

//start traversing the binary tree at // the root node

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

Data Structures Using Java

Nonrecursive Preorder Traversal General Algorithm


1. current = root; //start the traversal at the root node 2. while(current is not NULL or stack is nonempty) if(current is not NULL) { visit current; push current onto stack; current = current.llink; } else { pop stack into current; current = current.rlink; } //prepare to visit //the right subtree

31

Data Structures Using Java

Nonrecursive Postorder Traversal


1. 2. 3.

current = root; v = 0;

//start traversal at root node

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.

push current and 1 onto stack; current = current.llink; }

32

Data Structures Using Java

Nonrecursive Postorder Traversal (Continued)


else { pop stack into current and v; if(v == 1) { push current and 2 onto stack; current = current.rlink; v = 0; } else visit current; }

33

Data Structures Using Java

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

Data Structures Using Java

Perfectly Balanced Binary Tree

35

Data Structures Using Java

AVL (Height-Balanced Trees)


y An AVL tree (or height-balanced tree) is a binary search tree

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

Data Structures Using Java

AVL Trees

37

Data Structures Using Java

Non-AVL Trees

38

Data Structures Using Java

Balance Factor (bf)


y The balance factor of x, bf(x) is defined as:

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

Data Structures Using Java

Insertion Into AVL Tree

40

Data Structures Using Java

Insertion Into AVL Trees

41

Data Structures Using Java

Insertion Into AVL Trees

42

Data Structures Using Java

Insertion Into AVL Trees

43

Data Structures Using Java

Insertion Into AVL Trees

44

Data Structures Using Java

AVL Tree Rotations


y Reconstruction procedure: rotating tree y left rotation and right rotation y Suppose that the rotation occurs at node x y Left rotation: certain nodes from the right subtree of x move to its

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

Data Structures Using Java

AVL Tree Rotations

46

Data Structures Using Java

AVL Tree Rotations

47

Data Structures Using Java

AVL Tree Rotations

48

Data Structures Using Java

AVL Tree Rotations

49

Data Structures Using Java

AVL Tree Rotations

50

Data Structures Using Java

AVL Tree Rotations

51

Data Structures Using Java

S-ar putea să vă placă și