/// <summary> /// Método para buscar uma chave na estrutura /// </summary> /// <param name="key">Chave a ser buscada</param> /// <returns>Árvore contendo um nó com a chave, em caso de sucesso na busca, /// ou um nó com chave nula (nó onde a chave deveria estar)</returns> public override AbstractTree searchKey(Key key) { AbstractBinaryTree tree = null; if (KEY != null) { switch (key.VALUE.CompareTo(KEY.VALUE)) { case 0: tree = this; break; case 1: tree = (AbstractBinaryTree)getRightChild().searchKey(key); setThisRef(splayUpTree(this, tree.KEY)); break; case -1: tree = (AbstractBinaryTree)getLeftChild().searchKey(key); setThisRef(splayUpTree(this, tree.KEY)); break; } } else { tree = this; } return(tree); }
public AbstractBinaryTree zigZig(AbstractBinaryTree tree) { tree = zig(tree); tree = zig(tree); return(tree); }
public AbstractBinaryTree zagZig(AbstractBinaryTree tree) { tree.setLeftChild(zag((Splay)tree.getLeftChild())); tree = zig(tree); return(tree); }
public AbstractBinaryTree zagZag(AbstractBinaryTree tree) { tree = zag(tree); tree = zag(tree); return(tree); }
public AbstractBinaryTree zigZag(AbstractBinaryTree tree) { tree.setRightChild(zig((Splay)tree.getRightChild())); tree = zag(tree); return(tree); }
/// <summary> /// Método para Alterar a própria referência. /// </summary> /// <param name="tree">Nó para atualizar a referência atual</param> private void setThisRef(AbstractBinaryTree tree) { // Verifica se o nó atual já é igual ao nó passado. if (!this.Equals(tree)) { // Cria um nó auxiliar Splay aux = new Splay(); // Atualiza valores aux.KEY = KEY; aux.HEIGHT = HEIGHT; aux.setLeftChild(getLeftChild()); aux.setRightChild(getRightChild()); // Modifica a instância this, verificando se o valor do nó passado. switch (tree.KEY.VALUE.CompareTo(KEY.VALUE)) { // Caso seja maior, o auxiliar vira filho a direita. case 1: key = tree.KEY; height = tree.HEIGHT; setLeftChild(aux); setRightChild(tree.getRightChild()); break; // Caso seja menor, o auxiliar vira filho a esquerda. case -1: KEY = tree.KEY; height = tree.HEIGHT; setLeftChild(tree.getLeftChild()); setRightChild(aux); break; } } }
public void SmokeTest() { var tree = new AbstractBinaryTree(); tree.Root = new BinaryTreeNode <IComparable>() { Value = 1, Left = new BinaryTreeNode <IComparable>() { Value = 2, Left = new BinaryTreeNode <IComparable>() { Value = 4 }, Right = new BinaryTreeNode <IComparable>() { Value = 5 } }, Right = new BinaryTreeNode <IComparable>() { Value = 3, }, }; var result = tree.GetLevelOrderTraversal(); Assert.AreEqual(5, result.Count); Assert.AreEqual(1, result[0].Value); Assert.AreEqual(2, result[1].Value); Assert.AreEqual(3, result[2].Value); Assert.AreEqual(4, result[3].Value); Assert.AreEqual(5, result[4].Value); }
public BinaryVisitor(AbstractBinaryTree tree) { points = new Hashtable(); if (tree != null) { levelsCount = new int[tree.HEIGHT + 1]; } initPointsHT(tree); }
private void initPointsHT(AbstractBinaryTree tree) { if (tree.KEY != null) { points.Add(tree.KEY.VALUE, "" + tree.HEIGHT + "." + levelsCount[tree.HEIGHT]); levelsCount[tree.HEIGHT]++; initPointsHT(tree.getLeftChild()); initPointsHT(tree.getRightChild()); } }
public void drawTree(AbstractBinaryTree tree) { if (tree.KEY != null) { height++; int[] point = binVis.getPoint(tree.KEY.VALUE); drawNode(tree.KEY.VALUE, MAX_WIDTH / height, point[1]); drawTree(tree.getLeftChild()); } }
public AbstractBinaryTree splayUpTree(AbstractBinaryTree tree, Key key) { if (tree.KEY.VALUE.Equals(key.VALUE)) { } if (tree.getLeftChild().KEY != null) { if (tree.getLeftChild().getLeftChild().KEY != null) { if (tree.getLeftChild().getLeftChild().KEY.VALUE.Equals(key.VALUE)) { tree = zigZig(tree); return(tree); } } if (tree.getLeftChild().getRightChild().KEY != null) { if (tree.getLeftChild().getRightChild().KEY.VALUE.Equals(key.VALUE)) { tree = zagZig(tree); return(tree); } } if (tree.getLeftChild().KEY.VALUE.Equals(key.VALUE)) { tree = zig(tree); return(tree); } } if (tree.getRightChild().KEY != null) { if (tree.getRightChild().getRightChild().KEY != null) { if (tree.getRightChild().getRightChild().KEY.VALUE.Equals(key.VALUE)) { tree = zagZag(tree); return(tree); } } if (tree.getRightChild().getLeftChild().KEY != null) { if (tree.getRightChild().getLeftChild().KEY.VALUE.Equals(key.VALUE)) { tree = zigZag(tree); return(tree); } } if (tree.getRightChild().KEY.VALUE.Equals(key.VALUE)) { tree = zag(tree); return(tree); } } return(tree); }
private void ckbSplay_CheckedChanged(object sender, System.EventArgs e) { if (ckbSplay.Checked) { AbstractBinaryTree binaryTree = (AbstractBinaryTree)TreeHandler.getInstance().addTree(StructureType.SPLAY, 1); BinaryView splayView = new BinaryView(binaryTree); splayView.MdiParent = this; views.Add(splayView); insert(); splayView.Show(); } }
public BinaryView(AbstractBinaryTree tree) { InitializeComponent(); this.tree = tree; panelGraph = this.CreateGraphics(); defFont = new Font("Verdana", 8.0f); blackPen = new Pen(Color.Black); whitePen = new Pen(Color.White); treePen = new Pen(Color.LightGray); alterTreePen = new Pen(Color.Gray); MAX_HEIGHT = this.Height; MAX_WIDTH = Width; }
public AbstractBinaryTree zag(AbstractBinaryTree tree) { Splay aux = (Splay)tree; Splay rightChild = (Splay)tree.getRightChild(); Splay subTree2 = (Splay)rightChild.getLeftChild(); rightChild.setLeftChild(tree); tree.setRightChild(subTree2); updateHeight(tree); updateHeight(rightChild); return(rightChild); }
protected abstract TreeType CreateNode(AbstractBinaryTree<TreeType, T> left, T value, AbstractBinaryTree<TreeType, T> right);
private int updateHeight(AbstractBinaryTree tree) { tree.HEIGHT = Math.Max(tree.getRightChild().HEIGHT, tree.getLeftChild().HEIGHT) + 1; return(tree.HEIGHT); }