public static void Insert(TreeNodeGUI _root, int key) { if (_root == null) { return; } if (key > _root.data) { if (_root.rightNode == null) { _root.rightNode = new TreeNodeGUI(); _root.rightNode.data = key; } else { Insert(_root.rightNode, key); } } else if (key < _root.data) { if (_root.leftNode == null) { _root.leftNode = new TreeNodeGUI(); _root.leftNode.data = key; } else { Insert(_root.leftNode, key); } } }
public static void Delete(ref TreeNodeGUI _root, int _key) { if (_root == null) { return; } if (_root.data == _key) { if (_root.rightNode == null && _root.leftNode == null) { _root = null; } else if (_root.rightNode == null && _root.leftNode != null) { _root = _root.leftNode; } else if (_root.rightNode != null && _root.leftNode == null) { _root = _root.rightNode; } else { TreeNodeGUI parentNode = _root.leftNode; TreeNodeGUI curNode = _root.leftNode; while (curNode != null) { if (curNode.rightNode == null) { break; } parentNode = curNode; curNode = curNode.rightNode; } if (parentNode == curNode) { curNode.rightNode = _root.rightNode; _root = curNode; } else { parentNode.rightNode = curNode.leftNode; curNode.leftNode = _root.leftNode; curNode.rightNode = _root.rightNode; _root = curNode; } } return; } if (_root.data < _key) { Delete(ref _root.rightNode, _key); } else { Delete(ref _root.leftNode, _key); } }
public static void DLR(TreeNodeGUI _bsTree) { if (_bsTree == null) { return; } DLR(_bsTree.leftNode); DLR(_bsTree.rightNode); }
void CalculatePosition(TreeNodeGUI _root) { if (_root == null) { return; } _root.CalculatePosition(maxLayer); CalculatePosition(_root.leftNode); CalculatePosition(_root.rightNode); }
void OnGUI() { GUILayout.BeginHorizontal(); GUILayout.EndHorizontal(); TreeNodeGUI.wRatio = EditorGUILayout.FloatField("wRatio", TreeNodeGUI.wRatio); TreeNodeGUI.hRatio = EditorGUILayout.FloatField("hRatio", TreeNodeGUI.hRatio); EditorGUILayout.BeginHorizontal(); value = EditorGUILayout.IntField("Value", value); if (GUILayout.Button("Add Node")) { List <int> list = new List <int> { 50, 30, 70, 10, 40, 90, 80, 20, 22, 23, 24 }; for (int i = 0; i < list.Count; i++) { if (root == null) { root = new TreeNodeGUI(); root.data = list[i]; root.layer = 1; root.index = 1; } else { maxLayer = 0; BinaryTreeGUI.Insert(root, list[i]); BinaryTreeGUI.ResetIndex(root); BinaryTreeGUI.ResetLayer(root, ref maxLayer); } } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); value = EditorGUILayout.IntField("Value", value); if (GUILayout.Button("Del Node")) { BinaryTreeGUI.Delete(ref root, value); maxLayer = 0; root.index = 1; root.layer = 1; BinaryTreeGUI.ResetIndex(root); BinaryTreeGUI.ResetLayer(root, ref maxLayer); } EditorGUILayout.EndHorizontal(); CalculatePosition(root); DrawLine(root); DrawNode(root); }
void DrawLine(TreeNodeGUI _tree) { if (_tree == null) { return; } if (_tree.leftNode != null) { Handles.DrawLine(_tree.point, _tree.leftNode.point); DrawLine(_tree.leftNode); } if (_tree.rightNode != null) { Handles.DrawLine(_tree.point, _tree.rightNode.point); DrawLine(_tree.rightNode); } }
public static void ResetIndex(TreeNodeGUI _bsTree) { if (_bsTree == null) { return; } if (_bsTree.leftNode != null) { _bsTree.leftNode.index = _bsTree.index * 2; ResetIndex(_bsTree.leftNode); } if (_bsTree.rightNode != null) { _bsTree.rightNode.index = _bsTree.index * 2 + 1; ResetIndex(_bsTree.rightNode); } }
public static TreeNodeGUI Search(TreeNodeGUI _root, int _key) { if (_root == null) { return(null); } if (_root.data == _key) { return(_root); } if (_root.data < _key) { return(Search(_root.rightNode, _key)); } else { return(Search(_root.rightNode, _key)); } }
public static void ResetLayer(TreeNodeGUI _bsTree, ref int _maxLayer) { if (_bsTree == null) { return; } if (_bsTree.leftNode != null) { _bsTree.leftNode.layer = _bsTree.layer + 1; _maxLayer = Mathf.Max(_maxLayer, _bsTree.leftNode.layer); ResetLayer(_bsTree.leftNode, ref _maxLayer); } if (_bsTree.rightNode != null) { _bsTree.rightNode.layer = _bsTree.layer + 1; _maxLayer = Mathf.Max(_maxLayer, _bsTree.rightNode.layer); ResetLayer(_bsTree.rightNode, ref _maxLayer); } }
void DrawNode(TreeNodeGUI _tree) { if (_tree == null) { return; } Rect rect = new Rect(_tree.point.x - BSTree.nodeSize / 2, _tree.point.y - BSTree.nodeSize / 2, BSTree.nodeSize, BSTree.nodeSize); if (delvalue == _tree.data) { GUI.color = Color.red; } if (GUI.Button(rect, _tree.data.ToString())) { delvalue = _tree.data; } GUI.color = Color.white; DrawNode(_tree.leftNode); DrawNode(_tree.rightNode); }