1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

	class TreeNode
	{
		protected $data, $rightNode, $leftNode;
		
		protected function __construct($data)
		{
			$this->data = $data;
			$this->rightNode = NULL;
			$this->leftNode = NULL;
		}
	}
	
	class binTree extends TreeNode
	{
		private $root;
		public function __construct()
		{
			$this->root = new TreeNode(NULL);
		}
		
		public function addNode($data)
		{
			if($this->root->data == NULL) //then this is a new tree
			{
				$this->root->data = $data;
			}
			else
			{
				if($data <= $this->root->data) //if the new data is less than or equal to that stored in root, then store it in the left child node.
				{
					$this->addLeftNode($this->root->leftNode, $data);
				}
				else //it must be greater than. Store it in the right child node
				{
					$this->addRightNode($this->root->rightNode, $data);
				}
			}
		}
		private function addRightNode($node, $data)
		{
			if($node == NULL) //right child node doesn'texist. create a new one.
			{
				$node->rightNode = new TreeNode($data);
			}
			else
			{
				if($data <= $node->data)
				{
					$this->addLeftNode($node->leftNode, $data);
				}
				else
				{
					$this->addRightNode($node->rightNode, $data);
				}
			}
		}
		private function addLeftNode($node, $data)
		{
			if($node== NULL)
			{
				$node->leftNode = new TreeNode($data);
			}
			else
			{
				if($data <= $node->data)
				{
					$this->addLeftNode($node->leftNode, $data);
				}
				else
				{
					$this->addRightNode($node->rightNode, $data);
				}
			}
		}
		
		public function printTreeInOrder()
		{
			$this->print_inOrder($this->root);
		}
		public function printTreepreOrder()
		{
			$this->print_preOrder($this->root);
		}
		public function printTreePostOrder()
		{
			$this->print_postOrder($this->root);
		}
		private function print_postOrder($node)
		{
			if($node != NULL)
			{
				$this->print_postOrder($node->leftNode);
				$this->print_postOrder($node->rightNode);
				printf("%s, ", $node->data);
			}
		}
		private function print_preOrder($node)
		{
			if($node != NULL)
			{
				printf("%s, ", $node->data);
				$this->print_preOrder($node->leftNode);
				$this->print_preOrder($node->rightNode);
			}
		}
		private function print_inOrder($node)
		{
			if($node != NULL)
			{
				$this->print_inOrder($node->leftNode);
				printf("%s, ", $node->data);
				$this->print_inOrder($node->rightNode);
			}
		}
	}
	
	$tree = new binTree();
	$tree->addNode('F');
	$tree->addNode('B');
	$tree->addNode('G');
	$tree->addNode('A');
	$tree->addNode('D');
	$tree->addNode('I');
	$tree->addNode('C');
	$tree->addNode('E');
	$tree->addNode('H');
	echo "<br />In Order Traversal: ";
	$tree->printTreeInOrder();
	echo "<br />Pre-Order Traversal: ";
	$tree->printTreePreOrder();
	echo "<br /> Post-Order Traversal: ";
	$tree->printTreePostOrder();


?>